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 23eb148 commit f3a2e5dCopy full SHA for f3a2e5d
java/Binary Search/FindTheInsertionIndex.java
@@ -0,0 +1,20 @@
1
+public class FindTheInsertionIndex {
2
+ public int findTheInsertionIndex(int[] nums, int target) {
3
+ int left = 0;
4
+ int right = nums.length;
5
+ while (left < right) {
6
+ int mid = (left + right) / 2;
7
+ // If the midpoint value is greater than or equal to the target,
8
+ // the lower bound is either at the midpoint, or to its left.
9
+ if (nums[mid] >= target) {
10
+ right = mid;
11
+ }
12
+ // The midpoint value is less than the target, indicating the
13
+ // lower bound is somewhere to the right.
14
+ else {
15
+ left = mid + 1;
16
17
18
+ return left;
19
20
+}
0 commit comments