Maximum elements which can be crossed using given units of a and b | Function to find the number of elements crossed ; Keep a copy of a ; Iterate in the binary array ; If no a and b left to use ; If there is no a ; use b and increase a by 1 if arr [ i ] is 1 ; simply use b ; Use a if theres no b ; Increase a and use b if arr [ i ] == 1 ; Use a ; Driver code
def findElementsCrossed ( arr , a , b , n ) : NEW_LINE INDENT aa = a NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a == 0 and b == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( a == 0 ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT b -= 1 NEW_LINE a = min ( aa , a + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT b -= 1 NEW_LINE DEDENT DEDENT elif ( b == 0 ) : NEW_LINE INDENT a -= 1 NEW_LINE DEDENT elif ( arr [ i ] == 1 and a < aa ) : NEW_LINE INDENT b -= 1 NEW_LINE a = min ( aa , a + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT a -= 1 NEW_LINE DEDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 0 , 0 , 1 , 0 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE a = 1 NEW_LINE b = 2 NEW_LINE print ( findElementsCrossed ( arr , a , b , n ) ) NEW_LINE