Skip to content

Commit

Permalink
added Binary Search.cpp problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 authored Nov 29, 2022
1 parent 90c41a5 commit 6da281a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Binary Search/Binary Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int search(vector<int>& nums, int target) {
int i = 0, j = nums.size()-1, res = -1;
while(i<=j){
int mid = (i+j)/2;
if(nums[mid]==target){
return mid;
}
else if(nums[mid]>target){
j = mid-1;
}
else{
i=mid+1;
}
}

return res;
}
};

0 comments on commit 6da281a

Please sign in to comment.