Created
October 30, 2018 23:43
-
-
Save Indp-Dustin/d1b11b82fe55425f5d51ee2e1231d835 to your computer and use it in GitHub Desktop.
Counting Pairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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