InferenceAI

Enter at least two characters to search.

Start free

Sorted Position

easyBinary Search15 min10 XP

Given a sorted list of distinct integers nums, return the index of target, or -1 if it is absent.

Requirements

  • Run in O(log⁡n)O(\log n) time. Do not use list.index or the bisect module.

Examples

EXAMPLE WALKTHROUGHpresent

A guided view of the published example, not a trace of your code.

INPUT[[-1,0,3,5,9,12],9]
TASKsorted_position()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [-1,0,3,5,9,12], target = 9

Output: 4

Explanation: present

Input: nums = [-1,0,3,5,9,12], target = 2

Output: -1

Explanation: absent

Input: nums = [], target = 1

Output: -1

Explanation: empty

Hint 1

Keep a closed interval [lo, hi] and compare against the middle element.

Topics

DSABinary SearchPython