Search Insert Position

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Binary Search — Halve & Conquer

On a sorted range, throw away half each step.

int L = 0, R = n - 1;
while (L <= R) {
    int mid = L + (R - L) / 2;
    if (a[mid] == target) return mid;
    if (a[mid] < target) L = mid + 1;
    else R = mid - 1;
}
return -1;
Binary Search

Search Insert Position

Easy
LeetCode
2/3
EasyBinary SearchLeetCode #35

Search Insert Position

Lower bound: first index where nums[i] >= target.

Examples

Example 1

Input

4 5
1 3 5 6

Output

2
Example 2

Input

4 2
1 3 5 6

Output

1

Stuck? Take a nudge

C++17