-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathE0059.cpp
More file actions
34 lines (30 loc) · 677 Bytes
/
E0059.cpp
File metadata and controls
34 lines (30 loc) · 677 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
Problem Statement: https://www.hackerrank.com/challenges/beautiful-triplets/problem
*/
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int beautifulTriplets(int d, vector<int> arr) {
int count = 0;
unordered_set<int> hashSet;
for(int i=0 ; i<arr.size() ; i++){
bool isBeautifulTriplet = (hashSet.find(arr[i]-d) != hashSet.end()) && (hashSet.find(arr[i]-2*d) != hashSet.end());
if(isBeautifulTriplet)
count++;
hashSet.insert(arr[i]);
}
return count;
}
int main()
{
int n, d, num;
vector<int> arr;
cin>>n>>d;
for(int i=0 ; i<n ; i++){
cin>>num;
arr.push_back(num);
}
cout<<beautifulTriplets(d, arr);
return 0;
}