-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathThree_Sum.cpp
53 lines (45 loc) · 1.07 KB
/
Three_Sum.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
// Three_Sum
/*
Given an array of integers nums(sorted in ascending order) and an integer target,
return indices(zero indexed) of the three numbers if exists such that they sum adds up to target.
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> Three_Sum(vector<int> &arr, int target) {
for (int i = 0; i < arr.size() - 2; i++) {
int low = i + 1, high = arr.size() - 1;
while (low <= high) {
int sum = arr[low] + arr[high];
if (target == sum) {
return {i, low, high};
}
else if (target < sum) {
high--;
}
else {
low++;
}
}
}
return { -1, -1, -1};
}
int main()
{
cout << "Enter the size of the array: ";
int n;
cin >> n;
cout << "Enter the array: " << '\n';
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the target: ";
int target;
cin >> target;
vector<int> p = Three_Sum(arr, target);
if (p[0] == -1 && p[1] == -1 && p[2] == -1)
cout << "Three numbers not exist in array" << endl;
else
cout << "Indices are " << p[0] << ", " << p[1] << " and " << p[2] << endl;
return 0;
}