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 ; Given array arr [ ] ; Function call
function countPairs ( arr , n ) { let oneCount = 0 ; let desiredPair = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { oneCount ++ ; } if ( arr [ i ] == 0 ) { desiredPair += oneCount ; } } return desiredPair ; } let arr = [ 3 , 1 , 1 , 0 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;