Check if Decimal representation of given Binary String is divisible by K or not | Function to check the binary number is divisible by K ; Array poweroftwo will store pow ( 2 , i ) % k ; Initializing the first element in Array ; Storing every pow ( 2 , i ) % k value in the array ; To store the remaining ; Iterating till N ; If current bit is 1 ; Updating rem ; If completely divisible ; If not Completely divisible ; Given Input ; length of string s ; Function Call
function divisibleByk ( s , n , k ) { let poweroftwo = new Array ( n ) ; poweroftwo [ 0 ] = 1 % k ; for ( let i = 1 ; i < n ; i ++ ) { poweroftwo [ i ] = ( poweroftwo [ i - 1 ] * ( 2 % k ) ) % k ; } let rem = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ n - i - 1 ] == ' ' ) { rem += ( poweroftwo [ i ] ) ; rem %= k ; } } if ( rem == 0 ) { return " " ; } else return " " ; } let s = " " ; let k = 9 ; let n = s . length ; document . write ( divisibleByk ( s , n , k ) ) ;