Modulo of a large Binary String | 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
function getMod ( str , n , k ) { var pwrTwo = Array ( n ) ; pwrTwo [ 0 ] = 1 % k ; for ( var i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } var res = 0 ; var i = 0 , j = n - 1 ; while ( i < n ) { if ( str [ j ] == ' ' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } var str = " " ; var n = str . length ; var k = 45 ; document . write ( getMod ( str , n , k ) ) ;