We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ea8734b commit 651351dCopy full SHA for 651351d
Adhoc + Basic/binary_search_III.cpp
@@ -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
25
26
27
28
29
30
+ if (arr[mid] > target) { // we want a greater than target value.
31
+ result = mid - 1;
32
33
34
35
36
37
38
39
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments