-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpair sum
More file actions
54 lines (40 loc) · 1.15 KB
/
pair sum
File metadata and controls
54 lines (40 loc) · 1.15 KB
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
/*
You have been given an integer array A and a number K. Now, you need to find out whether any two different elements of the array A sum to the
number K. Two elements are considered to be different if they lie at different positions in the array. If there exists such a pair of numbers,
print "YES" (without quotes), else print "NO" without quotes.
Input Format:
The first line consists of two integers N, denoting the size of array A and K. The next line consists of N space separated integers denoting
the elements of the array A.
Output Format:
Print the required answer on a single line.
Constraints:
1≤N≤106
1≤K≤2∗106
1≤A[i]≤106
SAMPLE INPUT
5 9
1 2 3 4 5
SAMPLE OUTPUT
YES
Explanation
Here, A[4]+A[5]=4+5=9. So, the answer is YES.
*/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ll n, target, sum=0,flag=0; cin>>n>>target;
ll a[n];
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n); ll l=0,h=n-1;
while(l<h)
{
sum=a[l]+a[h];
if(sum==target) { flag=1; break; }
else if(sum>target) h--;
else l++;
}
if(flag==1) cout<<"YES";
else cout<<"NO";
}