2Brute2Best
πŸ”₯0

Search Insert Position

EasyΒ·LeetCode #35 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given an array nums of distinct numbers sorted from smallest to largest, and a number target.

If target is in the array, return its index. If it isn't, return the index where it would be inserted to keep the array sorted.

Your solution must run in O(log n) time.

Input

A sorted integer array nums with distinct values, and an integer target.

Output

The index of target if it's present; otherwise the index where it would go so the array stays sorted.

Example 1
Input: nums = [1, 3, 5, 6], target = 5
Output: 2

5 is already in the array, at index 2.

Example 2
Input: nums = [1, 3, 5, 6], target = 2
Output: 1

2 is missing. It belongs between 1 and 3, which is index 1.

Example 3
Input: nums = [1, 3, 5, 6], target = 7
Output: 4

7 is bigger than everything, so it goes at the very end: index 4 (= the array's length).

Constraints
  • β€’ 1 ≀ nums.length ≀ 10^4
  • β€’ βˆ’10^4 ≀ nums[i] ≀ 10^4
  • β€’ nums contains distinct values sorted in ascending order
  • β€’ βˆ’10^4 ≀ target ≀ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œsorted array, distinct values”
β†’ Values split cleanly into "< target" then "β‰₯ target" β†’ one boundary to find.
β€œindex where it would be inserted”
β†’ We need an answer even when the target is missing β†’ boundary search, not exact match.
β€œO(log n)”
β†’ Binary search, not a scan.
03

Approach Complexity Ladder

Brute Force β€” linear scan for the first β‰₯O(N)O(1)

Walk left to right; return the first index with nums[i] β‰₯ target

Optimal β€” binary search for the first β‰₯ (lower bound)O(log N)O(1)

Binary search for the boundary; when the loop ends, lo is the answer

Brute Force Intuition

Go through the array from the left. The first element that is β‰₯ target is exactly the answer: either it is the target, or it's the first element bigger than it, so the target belongs right there. If no element is β‰₯ target, return n.

Recognition rule for this sub-pattern:

Sorted array; find a value, or the first/last position where a condition becomes true.
Remember this Β· Binary Search
Trigger
Sorted data, "minimum X such that…", "maximum X such that…", rotated arrays, answers that are monotonic (if X works, X+1 also works).
Think
Is there a yes/no question whose answers look like NNNNYYYY? Then binary search for the first Y.
Key question
What is my check function, and is it monotonic?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.