-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2Sum.cpp
More file actions
47 lines (39 loc) · 842 Bytes
/
Copy path2Sum.cpp
File metadata and controls
47 lines (39 loc) · 842 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>
#include<vector>
#include<algorithm>
using namespace std;
// Search Rotated Sort Array
int BinarySearch(int arr[], int n, int target)
{
vector<int>ans;
int start = 0, end = n-1;
while(start<end)
{
if(arr[start]+arr[end]==target)
{
ans.push_back(start+1);
ans.push_back(end+1);
return ans;
}
else if(arr[start]+arr[end]<target)
start++;
else
end--;
}
return ans;
}
int main()
{
int arr[1000];
int n;
cout<<"Enter the number element array: ";
cin>>n;
cout<<"Enter the elements: ";
for(int i=0; i<n; i++)
cin>>arr[i];
int target;
cout<<"Enter the target: ";
cin>>target;
cout<<BinarySearch(arr,n,target)<<endl;
return 0;
}