Ctrl K

Missing From One to n

Easy
You are given an array values of length n, and every number in it lies between 1 and n. Some numbers may repeat, which means others may not appear at all. Return, in increasing order, every number from 1 to n that is absent from the array. Try to do it in O(n) time with no extra memory beyond the answer.
Example 1
values=[2, 2, 5, 1, 5] → [3, 4]

The length is 5, so the numbers 1 to 5 are expected. 1, 2 and 5 are there; 3 and 4 are missing.

Example 2
values=[3, 1, 2] → []

Every number from 1 to 3 is present, nothing is missing.

Example 3
values=[1, 1, 1, 1] → [2, 3, 4]
Constraints
  • 1 ≤ n = values.length ≤ 10⁵
  • 1 ≤ values[i] ≤ n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyRepeat Within Reach EasyThe Lost Ticket Easy
loading Python…
Input
values=[2, 2, 5, 1, 5]
Expected
[3, 4]

Run checks the first 3 tests in your browser. Submit checks all 58 tests on the server, hidden ones included.