Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int A[] = {1,2,3,4,5,6,7};
- int SearchAtLeast(int x){
- int left = 0, right = sizeof(A)/sizeof(A[0]), mid = 0, result = -1;
- while(left < right)
- {
- mid = (left + right)/2;
- if(A[mid] >= x){
- result = mid;
- right = mid - 1;
- }
- else {
- left = mid + 1;
- }
- }
- return result;
- }
- int SearchAtMost(int x){
- int left = 0, right = sizeof(A)/sizeof(A[0]), mid = 0, result = -1;
- while(left < right)
- {
- mid = (left + right)/2;
- if(A[mid] <= x){
- result = mid;
- left = mid + 1;
- }
- else {
- right = mid - 1;
- }
- }
- return result;
- }
- int main(){
- cout<<SearchAtLeast(-2)<<endl;
- cout<<SearchAtMost(10)<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement