Skip to main content

Binary Search

Given a sorted array nums and an integer target Return the index of target if it exists, otherwise return -1 Must use O(log n) time complexity Example: nums = [-1,0,3,5,9,12], target = 9 => 4

Find a target in a sorted array. It's the most fundamental divide-and-conquer algorithm, and getting the boundary conditions exactly right is trickier than it looks. Interviewers use it as a baseline—if you fumble binary search, they'll wonder about everything else.

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.

1Find Target in Sorted Arrayeasy
2Part 2

Each locked part opens in the editor once the part before it passes.

Based onLeetCode

Get a note when something new lands

New company guides, new problems and big changes to Coco. Rare, and you can leave any time. By joining you agree to get these emails.