Skip to content

Instantly share code, notes, and snippets.

@Indp-Dustin
Created October 30, 2018 23:43
Show Gist options
  • Save Indp-Dustin/d1b11b82fe55425f5d51ee2e1231d835 to your computer and use it in GitHub Desktop.
Save Indp-Dustin/d1b11b82fe55425f5d51ee2e1231d835 to your computer and use it in GitHub Desktop.
Counting Pairs
#include "pch.h"
#include <iostream>
#include <vector>
int pairs(int arr[], int n, int k)
{
int pairCount = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (i == j)
continue;
if (abs(arr[j] - arr[i]) == k)
{
pairCount++;
}
}
}
return pairCount;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
std::cout << pairs(arr, n, 1) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment