Function to calculate maximum splits possible
void max_segments ( string str , int K ) { int n = str . length ( ) ; int s = 0 , sum = 0 , count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int a = str [ i ] - '0' ; sum += a * pow ( 2 , s ) ; s ++ ; if ( sum != 0 && sum % K == 0 ) { count ++ ; sum = 0 ; s = 0 ; } } if ( sum != 0 ) cout << " - 1" << endl ; else cout << count << endl ; }