Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | Java program for the above approach ; Function to count pairs from an array satisfying given conditions ; Stores the total count of pairs ; Stores count of a [ i ] / i ; Traverse the array ; Updating count ; Update frequency in the Map ; Print count of pairs ; Driver Code ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
import java . util . * ; class GFG { static void countPairs ( int [ ] arr , int N ) { int count = 0 ; Map < Double , Integer > mp = new HashMap < Double , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Double val = 1.0 * arr [ i ] ; Double idx = 1.0 * ( i + 1 ) ; if ( mp . containsKey ( val / idx ) ) count += mp . get ( val / idx ) ; if ( mp . containsKey ( val / idx ) ) mp . put ( val / idx , mp . getOrDefault ( val / idx , 0 ) + 1 ) ; else mp . put ( val / idx , 1 ) ; } System . out . print ( count ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 3 , 5 , 6 , 5 } ; int N = arr . length ; countPairs ( arr , N ) ; } }