-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathsum_of_three_values.c++
57 lines (48 loc) · 1.11 KB
/
sum_of_three_values.c++
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
// Given an array of size n. Your task is to find three values (at distinct positions) whose sum is x.
// (1<=n<=5000, 1<=x<=10^9)
// Input:
// 4 8
// 2 7 5 1
// Output:
// 1 3 4
// Time complexity: O(n^2)
// Concepts Involved: Sorting, Pointers
#include<bits/stdc++.h>
#define fl(i,a,b) for(i=a;i<b;i++)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long int ll;
int main()
{
fast;
ll n,k, target, l, r; cin>>n>>k;
vector<pair<ll,ll>> a;
pair<ll, ll > p;
for(ll i=0; i<n; i++)
{
cin>>p.first;
p.second=i+1;
a.push_back(p);
}
sort(begin(a), end(a));
for (ll i = 0; i < n; i++)
{
l=0;
r=n-1;
target= k-a[i].first;
while(l<r)
{
if(a[l].first+a[r].first==target && i!=l && l!=r && r!=i)
{
cout<<a[l].second<<" "<<a[r].second<<" "<<a[i].second;
return 0;
}
else if(a[l].first+a[r].first<=target)
l++;
else
r--;
}
}
cout<<"IMPOSSIBLE";
return 0;
}