Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | C ++ 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 values of x in range [ 1 , N ] . ; Iterating over all values of y that are divisible by x in the range [ 1 , N ] . ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int x = 1 ; x <= n ; x ++ ) { for ( int y = 2 * x ; y <= n ; y += x ) { if ( ( arr [ y - 1 ] % arr [ x - 1 ] == 0 ) && ( arr [ y - 1 ] / arr [ x - 1 ] == y / x ) ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 5 , -2 , 4 , 20 , 25 , -6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , n ) ; return 0 ; }