Minimize remaining array sizes by removing equal pairs of first array elements | Function to count the remaining elements in the arrays ; Stores the count of 1 s in L1 ; Store the count of 0 s in L2 ; Traverse the array L1 ; If condition is True ; Increment one by 1 ; Increment zero by 1 ; Stores the index after which no further removals are possible ; Traverse the array L2 ; If condition is True ; Decrement one by 1 ; If one < 0 , then break out of the loop ; Decrement zero by 1 ; If zero < 0 , then break out of loop ; Print the answer ; Driver Code ; Function Call
def countRemainingElements ( L1 , L2 , n ) : NEW_LINE INDENT one = 0 ; NEW_LINE zero = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( L1 [ i ] == 1 ) : NEW_LINE INDENT one += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT zero += 1 ; NEW_LINE DEDENT DEDENT ans = n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( L2 [ i ] == 1 ) : NEW_LINE INDENT one -= 1 ; NEW_LINE if ( one < 0 ) : NEW_LINE INDENT ans = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT zero -= 1 ; NEW_LINE if ( zero < 0 ) : NEW_LINE INDENT ans = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT print ( n - ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L1 = [ 1 , 1 , 0 , 0 ] ; NEW_LINE L2 = [ 0 , 0 , 0 , 1 ] ; NEW_LINE N = len ( L1 ) ; NEW_LINE countRemainingElements ( L1 , L2 , N ) ; NEW_LINE DEDENT