-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy pathFind Pair Given Difference.cpp
62 lines (52 loc) · 1.29 KB
/
Find Pair Given Difference.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Find Pair Given Difference
==========================
Given an unsorted array Arr[] and a number N. You need to write a program to find if there exists a pair of elements in the array whose difference is N.
Input:
First line of input contains an integer T which denotes the number of test cases. Then T test cases follows. First line of each test case contains two space separated integers L and N where L denotes the length of array or the number of elements in the array and N denotes the difference between two elements. Second line of each test case contains L space separated integers which denotes the elements of the array.
Output:
For each test case, in a new line print 1 if the pair is found otherwise print -1 if there does not exist any such pair.
Constraints:
1<=T<=100
1<=L<=104
1<=Arr[i]<=105
Example:
Input:
2
6 78
5 20 3 2 5 80
5 45
90 70 20 80 50
Output:
1
-1
*/
#include <bits/stdc++.h>
using namespace std;
int solve()
{
int l, n;
cin >> l >> n;
vector<int> arr(l, 0);
for (auto &i : arr)
cin >> i;
unordered_set<int> s;
for (auto &i : arr)
{
if (s.find(abs(n - i)) != s.end() || s.find(n + i) != s.end())
return 1;
else
s.insert(i);
}
return -1;
}
int main()
{
int t;
cin >> t;
while (t--)
{
cout << solve() << endl;
}
return 0;
}