Given a sorted array of distinct integers and a target, return the index if found. If not, return the index where it would be inserted to keep the array sorted.
This is binary search with a twist — when the target isn't found, left ends up at the correct insertion position. Think about why!
💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!
search_insert([1,3,5,6], 5)
2
search_insert([1,3,5,6], 2)
1
search_insert([1,3,5,6], 7)
4