Count pairs with product of indices equal to the product of elements present at those indices | Java program for the above approach ; Function to count the number of pairs having product of indices equal to the product of elements at that indices ; Stores the count of valid pairs ; Generate all possible pairs ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver Code
import java . util . * ; class GFG { static int CountPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( i * j ) == ( arr [ i ] * arr [ j ] ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 3 , 2 , 6 } ; int N = arr . length ; System . out . println ( CountPairs ( arr , N ) ) ; } }