Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | Java program of the above approach ; Function of find the count of unordered pairs ( i , j ) in the array such that arr [ j ] / arr [ i ] = j / i . ; Stores the count of valid pairs ; Iterating over all possible pairs ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
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 ( ( arr [ j ] % arr [ i ] == 0 ) && ( j + 1 ) % ( i + 1 ) == 0 && ( arr [ j ] / arr [ i ] == ( j + 1 ) / ( i + 1 ) ) ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 2 , 4 , 20 , 25 , - 6 } ; int n = arr . length ; System . out . print ( countPairs ( arr , n ) ) ; } }