Skip to content

Instantly share code, notes, and snippets.

@mghamsar
Created September 4, 2016 21:08
Show Gist options
  • Save mghamsar/af194f693c63a99cc80bef1e8d087c0a to your computer and use it in GitHub Desktop.
Save mghamsar/af194f693c63a99cc80bef1e8d087c0a to your computer and use it in GitHub Desktop.
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 )
count++;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment