Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 651351d

Browse files
committed
added binary search III
1 parent ea8734b commit 651351d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎Adhoc + Basic/binary_search_III.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int lowerBound(vector<int> arr, int target) {
6+
int left = 0;
7+
int right = arr.size() - 1;
8+
int result = -1;
9+
10+
while (left <= right) {
11+
int mid = left + (right-left)/2; // To help with out of bounds
12+
if (arr[mid] >= target) { // we want the value in ans if the target exists
13+
result = mid;
14+
right = mid - 1;
15+
}
16+
else {
17+
left = mid + 1;
18+
}
19+
}
20+
return result;
21+
}
22+
23+
int upperBound(vector<int> arr, int target) {
24+
int left = 0;
25+
int right = arr.size() - 1;
26+
int result = -1;
27+
28+
while (left <= right) {
29+
int mid = left + (right-left)/2; // To help with out of bounds
30+
if (arr[mid] > target) { // we want a greater than target value.
31+
result = mid - 1;
32+
right = mid - 1;
33+
}
34+
else {
35+
left = mid + 1;
36+
}
37+
}
38+
return result;
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /