Count pairs ( i , j ) from an array such that i < j and arr [ j ] | Function to count the number of pairs ( i , j ) such that i < j and arr [ i ] - arr [ j ] = X * ( j - i ) ; Stores the count of all such pairs that satisfies the condition . ; Stores count of distinct values of arr [ i ] - x * i ; Iterate over the Map ; Increase count of pairs ; Print the count of such pairs ; Driver Code
def countPairs ( arr , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] - x * i ) in mp ) : NEW_LINE INDENT mp [ arr [ i ] - x * i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] - x * i ] = 1 NEW_LINE DEDENT DEDENT for key , value in mp . items ( ) : NEW_LINE INDENT n = value NEW_LINE count += ( n * ( n - 1 ) ) // 2 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE x = 3 NEW_LINE arr = [ 5 , 4 , 8 , 11 , 13 , 16 ] NEW_LINE countPairs ( arr , n , x ) NEW_LINE DEDENT