-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathupper_bound.cpp
More file actions
32 lines (30 loc) · 604 Bytes
/
upper_bound.cpp
File metadata and controls
32 lines (30 loc) · 604 Bytes
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
#include<iostream>
#include <vector>
using namespace std;
int upper_bound(vector<int> nums, int target)
{
int start = 0;
int end = nums.size()-1;
int mid = 0;
while(start<end)
{
mid = start + (end-start)/2;
if(nums[mid]<=target) start = mid+1;
else end = mid;
}
return start;
}
int main()
{
vector<int> nums;
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);
nums.push_back(4);
nums.push_back(6);
nums.push_back(6);
nums.push_back(8);
nums.push_back(11);
cout<<upper_bound(nums,6);
return 0;
}