PracticeSearching

Missing Score Milestone

MediumSearchingbinary searchinvariantscomparison Time not estimated Not started

Context

In the game Neon Climb, completed score milestones are stored in increasing order. Milestone 0 is the first possible milestone, and later milestones normally increase one at a time. One milestone may be absent. Determine the smallest nonnegative milestone number that does not appear in the stored list.

Problem

Given the increasing list of completed score milestones, return the smallest nonnegative integer that is absent. Use the list's index relationship with the expected consecutive milestone value to guide a recursive binary search. If a midpoint still matches the consecutive pattern, the missing milestone must be later. Otherwise, keep the earlier half where the first gap can occur. An empty list means milestone 0 is missing, while a complete prefix returns the next milestone after that prefix.

Examples

Example 1
Input: milestones = []
Output: 0
Explanation: For the empty list, the executed result is 0, which is the required missing milestone.
Example 2
Input: milestones = [0, 1, 2, 4, 5]
Output: 3
Explanation: For [0, 1, 2, 4, 5], the executed result is 3.
Example 3
Input: milestones = [0, 1, 2, 3]
Output: 4
Explanation: For [0, 1, 2, 3], the executed result is 4.

Constraints

  • 0 <= len(milestones) <= 100000
  • 0 <= milestones[i] <= 1000000 for every i
  • milestones: strictly increasing
  • Types: milestones is int[]; result is int

Function signature

def find_missing_milestone(milestones: list[int]) -> int
milestones list[int]
Increasing nonnegative milestone numbers recorded by the game.
returns int
The smallest nonnegative integer that does not occur in milestones.

Adapted from MBPP problem task_627 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Notes

  • The recorded milestones are guaranteed to be strictly increasing, so repeated values do not occur.
  • The missing value may be before the first entry, between entries, or immediately after the final entry.
Approved · cb052-abs_27_v212 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1