Minimum swaps required to make a binary string divisible by 2 ^ k | Function to return the minimum swaps required ; To store the final answer ; To store the count of one and zero ; Loop from end of the string ; If s [ i ] = 1 ; If s [ i ] = 0 ; If c_zero = k ; If the result can 't  be achieved ; Return the final answer ; Driver code
function findMinSwaps ( s , k ) { var ans = 0 ; var c_one = 0 , c_zero = 0 ; for ( var i = s . length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) c_one ++ ; if ( s [ i ] == ' ' ) c_zero ++ , ans += c_one ; if ( c_zero == k ) break ; } if ( c_zero < k ) return - 1 ; return ans ; } var s = " " ; var k = 2 ; document . write ( findMinSwaps ( s , k ) ) ;