Modulo of a large Binary String | C # implementation of the above approach ; Function to return the value of ( str % k ) ; pwrTwo [ i ] will store ( ( 2 ^ i ) % k ) ; To store the result ; If current bit is 1 ; Add the current power of 2 ; Driver code
using System ; class GFG { static int getMod ( string str , int n , int k ) { int i ; int [ ] pwrTwo = new int [ n ] ; pwrTwo [ 0 ] = 1 % k ; for ( i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } int res = 0 ; i = 0 ; int j = n - 1 ; while ( i < n ) { if ( str [ j ] == '1' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } public static void Main ( ) { string str = "1101" ; int n = str . Length ; int k = 45 ; Console . Write ( getMod ( str , n , k ) ) ; } }