-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeBinarySearch.cpp
80 lines (65 loc) · 1.71 KB
/
CodeBinarySearch.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// You are given an integer array 'A' of size 'N', sorted in ascending order. You are also given an integer 'target'.
// Your task is to write a function to search for 'target' in the array 'A'. If it exists, return its index in 0-based indexing. Otherwise, return -1.
// Note: You must write an algorithm whose time complexity is O(logN).
// Example:
// Input: ‘N’ = 7 ‘target’ = 3
// ‘A’ = [1, 3, 7, 9, 11, 12, 45]
// Output: 1
// Explanation: For A = [1, 3, 7, 9, 11, 12, 45],
// The index of element '3' is 1.
// Hence, the answer is '1'.
// My Code:
int search(vector<int>& nums, int target) {
int start = 0;
int end = nums.size()-1;
while (start <= end){
int mid = (start+end)/2;
if (nums[mid] == target)
return mid;
else if(target < nums[mid])
end = mid-1;
else
start = mid+1;
}
return -1;
}
// Main Code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include "solution.h"
class Runner
{
int t = 1;
vector<int> v;
int x;
public:
void takeInput()
{
int n; cin >> n;
assert(1 <= n && n <= (int)1e4);
v.assign(n, 0);
for(int i = 0; i < n; ++i) cin >> v[i];
cin >> x;
}
void execute()
{
vector<int> copy = v;
search(copy, x);
}
void executeAndPrintOutput()
{
vector<int> copy = v;
int ans = search(copy, x);
cout << ans << '\n';
}
};
int main()
{
// freopen("./Testcases/large/in/input11.txt", "r", stdin);
// freopen("./Testcases/large/out/output11.txt", "w", stdout);
Runner runner;
runner.takeInput();
runner.executeAndPrintOutput();
return 0;
}