forked from ShreyaDhir/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4sum.cpp
41 lines (40 loc) · 911 Bytes
/
4sum.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
#include <bits/stdc++.h>
using namespace std;
void findFourElements(int arr[], int n, int X)
{
unordered_map<int, pair<int, int> > mp;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
mp[arr[i] + arr[j]] = { i, j };
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
int sum = arr[i] + arr[j];
if (mp.find(X - sum) != mp.end())
{
pair<int, int> p = mp[X - sum];
if (p.first != i && p.first != j
&& p.second != i && p.second != j) {
cout << arr[i] << ", "
<< arr[j] << ", "
<< arr[p.first] << ", "
<< arr[p.second];
return;
}
}
}
}
}
int main()
{
int n;
cin>>n;
int *arr = new int[n];
for(int i = 0; i < n; ++i)
cin>>arr[i];
int X;
cin>>X;
findFourElements(arr, n, X);
return 0;
}