Smallest non | Function to return the length of the smallest substring divisible by 2 ^ k ; To store the final answer ; Left pointer ; Right pointer ; Count of the number of zeros and ones in the current substring ; Loop for two pointers ; Condition satisfied ; Updated the answer ; Update the pointer and count ; Update the pointer and count ; Driver code
function findLength ( s , k ) { var ans = 1000000000 ; var l = 0 ; var r = 0 ; var cnt_zero = 0 , cnt_one = 0 ; while ( l < s . length && r <= s . length ) { if ( cnt_zero >= k && cnt_one >= 1 ) { ans = Math . min ( ans , r - l ) ; l ++ ; if ( s [ l - 1 ] == ' ' ) cnt_zero -- ; else cnt_one -- ; } else { if ( r == s . length ) break ; if ( s [ r ] == ' ' ) cnt_zero ++ ; else cnt_one ++ ; r ++ ; } } if ( ans == 1000000000 ) return - 1 ; return ans ; } var s = " " ; var k = 2 ; document . write ( findLength ( s , k ) ) ;