Smallest Missing Non-negative Integer After Operations

You are given a 0-indexed integer array nums and an integer value.

In one operation, you can add or subtract value from any element of nums.

The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.

Return the maximum MEX of nums after applying the mentioned operation any number of times.

Example 1
Inputnums = [1,-10,7,13,6,8], value = 5
Output4
By transforming elements to include 0, 1, 2, and 3, the MEX becomes 4, and it can be shown that 4 is the maximum achievable MEX.
Example 2
Inputnums = [1,-10,7,13,6,8], value = 7
Output2
By subtracting 7 from nums[2], the array can include 0, making the MEX 2, and it can be shown that 2 is the maximum achievable MEX.

Constraints

  • 1 <= nums.length, value <= 10^5
  • -10^9 <= nums[i] <= 10^9

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate