Skip to content

ORIGINAL CODING QUESTION

First positive inventory gap

Return the smallest positive integer absent from the supplied values.

Open practice workspace

Original exercise · hard. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n integer values. Print one integer as described.

Constraints

1 ≤ n ≤ 200; -100 ≤ value ≤ 100. Inputs contain exactly the stated values.

Example 1

Input

4 3 4 -1 1

Output

2

The first absent positive is 2.

Example 2

Input

3 1 2 3

Output

4

The first absent positive is 4.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Only values 1 through n+1 can determine the answer.

Hint 2

Map a valid positive value to its corresponding index.

Hint 3

After placing or marking relevant values, scan for the first absent position.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →