Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Iterate over the range [ 0 , N ) ; Now , iterate from the value arr [ i ] - ( i % arr [ i ] ) till N with an increment of arr [ i ] ; If the given criteria satisfy then increment the value of count ; Return the resultant count ; Driver Code
function getPairsCount ( arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = arr [ i ] - ( i % arr [ i ] ) ; j < n ; j += arr [ i ] ) { if ( i < j && arr [ i ] * arr [ j ] == Math . abs ( i - j ) ) { count ++ ; } } } return count ; } let arr = [ 1 , 1 , 2 , 4 ] ; let N = arr . length ; document . write ( getPairsCount ( arr , N ) ) ;