Function to calculate maximum splits possible
def max_segments ( st , K ) : NEW_LINE INDENT n = len ( st ) NEW_LINE s , sum , count = 0 , 0 , 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT a = ord ( st [ i ] ) - 48 NEW_LINE sum += a * pow ( 2 , s ) NEW_LINE s += 1 NEW_LINE if ( sum != 0 and sum % K == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE sum = 0 NEW_LINE s = 0 NEW_LINE DEDENT DEDENT if ( sum != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( count ) NEW_LINE DEDENT DEDENT