Skip to content

Commit 9d0fae6

Browse files
Merge pull request #2 from Shubham2816/newcodes
hactoberfest-2022 solution binary search leetcode
2 parents 9751fd5 + 2b268b4 commit 9d0fae6

File tree

1 file changed

+13
-0
lines changed
  • 704.BinarySearch

1 file changed

+13
-0
lines changed

704.BinarySearch/.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int search(vector<int>& a, int k) {
4+
int low = 0, high = a.size() - 1, mid;
5+
while(low <= high){
6+
mid = (low + high) / 2;
7+
if(a[mid] == k)return mid;
8+
else if(a[mid] > k)high = mid - 1;
9+
else low = mid + 1;
10+
}
11+
return -1;
12+
}
13+
};

0 commit comments

Comments
 (0)