-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.cpp
More file actions
47 lines (41 loc) · 967 Bytes
/
Copy pathtest2.cpp
File metadata and controls
47 lines (41 loc) · 967 Bytes
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
#include<iostream>
using namespace std
int searchMaxIndex(int* arr, int val) {
int i = 0;
for(i = 0; i < arr.length(); i++) {
if(arr[i] >= val) {
break;
}
}
return i < arr.length() ? i : arr.length() - 1;
}
bool function main(int* arr, int val) {
if(arr[0] >= val) {
cout << -1;
return false;
}
int max_index = searchMaxIndex(arr, val);
int start = 0;
int end = max_index;
bool success = false;
while(!success && end < arr.length()) {
int res = arr[start] + arr[end];
if(res == val) {
cout << start << end;
return false;
} else if(res > val) {
end--;
if(end == start) {
cout << -1;
return false;
}
} else {
start++;
if(start == end) {
end++;
}
}
}
cout << -1;
return false;
}