Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | 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
def countPairs ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for x in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT for y in range ( 2 * x , n + 1 , x ) : NEW_LINE INDENT if ( ( arr [ y - 1 ] % arr [ x - 1 ] == 0 ) and ( arr [ y - 1 ] // arr [ x - 1 ] == y // x ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE DEDENT