Count pairs from an array having product of their sum and difference equal to 1 | Function to count the desired number of pairs ; Initialize oneCount ; Initialize the desiredPair ; Traverse the given array ; If 1 is encountered ; If 0 is encountered ; Update count of pairs ; Return the final count ; Driver Code ; Given array arr [ ] ; Function call
def countPairs ( arr , n ) : NEW_LINE INDENT oneCount = 0 NEW_LINE desiredPair = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT oneCount += 1 NEW_LINE DEDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT desiredPair += oneCount NEW_LINE DEDENT DEDENT return desiredPair NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT