Count pairs from an array having product of their sum and difference equal to 1 | Java program for the above approach ; 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
import java . io . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int oneCount = 0 ; int desiredPair = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { oneCount ++ ; } if ( arr [ i ] == 0 ) { desiredPair += oneCount ; } } return desiredPair ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 1 , 0 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }