Count pairs from an array having equal sum and quotient | Java program for the above approach ; Function to find number of pairs with equal sum and quotient from a given array ; Store the count of pairs ; Stores frequencies ; Traverse the array ; If y is neither 1 or 0 ; Evaluate x ; Increment count by frequency of x ; Update map ; Print the final count ; Driver Code ; Function Call
import java . util . * ; class GFG { static int countPairs ( int a [ ] , int n ) { int count = 0 ; HashMap < Double , Integer > mp = new HashMap < Double , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { double y = a [ i ] ; if ( y != 0 && y != 1 ) { double x = ( ( y * 1.0 ) / ( 1 - y ) ) * y ; if ( mp . containsKey ( x ) ) count += mp . get ( x ) ; } if ( mp . containsKey ( y ) ) { mp . put ( y , mp . get ( y ) + 1 ) ; } else { mp . put ( y , 1 ) ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { - 4 , - 3 , 0 , 2 , 1 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }