Created
May 4, 2016 21:10
-
-
Save cruxrebels/78dcfe5227d0e88fd9e3c5621b1164f3 to your computer and use it in GitHub Desktop.
Binary search for searching a range.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a sorted array of integers, find the starting and ending position of a given target value. | |
Your algorithm’s runtime complexity must be in the order of O(log n). | |
If the target is not found in the array, return [-1, -1]. | |
Example: | |
Given [5, 7, 7, 8, 8, 10] | |
and target value 8, | |
return [3, 4]. | |
https://www.interviewbit.com/problems/search-for-a-range/ | |
*/ | |
int Pos(const vector<int> &A, int B, bool flag) | |
{ | |
int start = 0, end = A.size()-1; | |
int result = -1; | |
while (start<=end) | |
{ | |
int mid = start + (end - start)/2; | |
if(A[mid]==B) | |
{ | |
result = mid; | |
if(flag) | |
end = mid - 1; | |
else | |
start = mid + 1; | |
} | |
else if(A[mid]<B) start = mid + 1; | |
else end = mid - 1; | |
} | |
return result; | |
} | |
vector<int> Solution::searchRange(const vector<int> &A, int B) { | |
vector<int> result; | |
result.emplace_back(Pos(A, B, true)); | |
result.emplace_back(Pos(A, B, false)); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment