All problems
0347EasyArrayHash TableSorting

First Free Locker Code

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2996Smallest Missing Integer Greater Than Sequential Prefix Sum

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A gym's handover log lists the locker codes handed out today, in the order they were handed out, as nums.

Look at the front of the log. A run of codes starting at the very first entry is called a ladder when every code in it is exactly one above the code before it; the first entry on its own always counts as a ladder. Let total be the sum of the codes in the longest ladder the log begins with.

Return the smallest integer that is at least total and does not appear anywhere in nums.

Examples

Example 1

Input
nums = [4,5,6,2,9]
Output
15

The log begins with the ladder 4, 5, 6, because 2 does not follow 6. Its codes add up to 15, and 15 never appears in the log.

Example 2

Input
nums = [7,8,3,15,16,17]
Output
18

The longest ladder at the front is 7, 8, which totals 15. The log already shows 15, 16 and 17, so the first value at or above 15 that is free is 18.

Example 3

Input
nums = [9,20]
Output
10

20 does not follow 9, so the ladder holds only the code 9 and the total is 9. The log shows 9 itself, and 10 is free.

Constraints

  • 1 <= nums.length <= 50
  • 1 <= nums[i] <= 50

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def missing_integer(nums: list[int]) -> int:
Java
public int missingInteger(int[] nums)
September 7
Apply