Count pairs ( i , j ) from an array such that i < j and arr [ j ] | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int n , int x ) { int count = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] - x * i ) ) mp [ arr [ i ] - x * i ] = 0 ; mp [ arr [ i ] - x * i ] = mp [ arr [ i ] - x * i ] + 1 ; } foreach ( KeyValuePair < int , int > v in mp ) { count += ( v . Value * ( v . Value - 1 ) ) / 2 ; } Console . WriteLine ( count ) ; } public static void Main ( string [ ] args ) { int n = 6 , x = 3 ; int [ ] arr = { 5 , 4 , 8 , 11 , 13 , 16 } ; countPairs ( arr , n , x ) ; } }