Skip to content

Instantly share code, notes, and snippets.

@christianroman
Created December 7, 2013 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianroman/7838331 to your computer and use it in GitHub Desktop.
Save christianroman/7838331 to your computer and use it in GitHub Desktop.
HackerRank challenges: Pairs https://www.hackerrank.com/challenges/pairs
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
/* Head ends here */
int pairs(vector < int > a,int k) {
int ans = 0;
sort(a.begin(), a.end());
for(std::vector<int>::size_type i = 0; i != a.size(); i++)
if (std::binary_search (a.begin(), a.end(), a[i] + k))
ans++;
return ans;
}
/* Tail starts here */
int main() {
int res;
int _a_size,_k;
cin >> _a_size>>_k;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
vector<int> _a;
int _a_item;
for(int _a_i=0; _a_i<_a_size; _a_i++) {
cin >> _a_item;
_a.push_back(_a_item);
}
res = pairs(_a,_k);
cout << res;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment