Ctrl K

Largest MEX After Shifts

Medium
You are given an integer array values and a positive integer step. In one move you may pick any element and either add step to it or subtract step from it. You may make any number of moves, including none. The **MEX** of an array is the smallest non-negative integer that does not appear in it. For example, the MEX of [3, 0, -2] is 1, and the MEX of [1, 2] is 0. Return the largest MEX you can reach with these moves.
Example 1
values=[8, -3, 6, 1, 3], step=4 → 4

8 → 0, -3 → 1, 6 → 2, 3 → 3. Getting 4 would need another element with remainder 0 modulo 4, but the only one (8) is already used for 0. The answer is 4.

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

With a step of 1 any element can become any number: make 0, 1 and 2, which gives 3.

Example 3
values=[5, 10], step=5 → 1
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
  • 1 ≤ step ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Guests by Lanyard Color MediumVertical Mirror Line MediumEquidistant Point Triples MediumA Stretch Divisible by k MediumJobs With a Cooldown Medium
loading Python…
Input
values=[8, -3, 6, 1, 3], step=4
Expected
4

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