Maximum splits in binary string such that each substring is divisible by given odd number | Function to calculate maximum splits possible ; Driver code
function rearrange ( str , K ) { var n = str . length ; var s = 0 , sum = 0 , count = 0 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { var a = str . charAt ( i ) - ' ' ; sum += a * Math . pow ( 2 , s ) ; s ++ ; if ( sum != 0 && sum % K == 0 ) { count ++ ; sum = 0 ; s = 0 ; } } if ( sum != 0 ) document . write ( " " ) ; else document . write ( count ) ; } var str = " " ; var K = 5 ; rearrange ( str , K ) ;