Skip to content

Instantly share code, notes, and snippets.

View mghamsar's full-sized avatar

Mahtab Ghamsari mghamsar

View GitHub Profile
def countPairsWithDiffK(arr, n, k):
count = 0
for i in range(0,n):
for j in range(i,n):
if (arr[i] - arr[j] == k or arr[j] - arr[i] == k ):
count = count+1
return count
@mghamsar
mghamsar / kdiff
Created September 4, 2016 21:08
Sample codes
int countPairsWithDiffK(int arr[], int n, int k)
{
int count = 0;
// Pick all elements one by one
for (int i = 0; i < n; i++)
{
// See if there is a pair of this picked element
for (int j = i+1; j < n; j++)
if (arr[i] - arr[j] == k || arr[j] - arr[i] == k )