Smallest non | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLength ( string s , int k ) { int ans = INT_MAX ; int l = 0 ; int r = 0 ; int cnt_zero = 0 , cnt_one = 0 ; while ( l < s . size ( ) and r <= s . size ( ) ) { if ( cnt_zero >= k and cnt_one >= 1 ) { ans = min ( ans , r - l ) ; l ++ ; if ( s [ l - 1 ] == '0' ) cnt_zero -- ; else cnt_one -- ; } else { if ( r == s . size ( ) ) break ; if ( s [ r ] == '0' ) cnt_zero ++ ; else cnt_one ++ ; r ++ ; } } if ( ans == INT_MAX ) return -1 ; return ans ; } int main ( ) { string s = "100" ; int k = 2 ; cout << findLength ( s , k ) ; return 0 ; }