Pairs with Difference less than K | C # code to find count of Pairs with difference less than K . ; to sort the array . ; Keep incrementing result while subsequent elements are within limits . ; Driver code
using System ; class GFG { static int countPairs ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && a [ j ] - a [ i ] < k ) { res ++ ; j ++ ; } } return res ; } public static void Main ( ) { int [ ] a = { 1 , 10 , 4 , 2 } ; int k = 3 ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n , k ) ) ; } }