Smallest Missing Non-negative Integer After Operations - Problem
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.
For example, if nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] to make nums = [-1,2,3].
The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.
For example, the MEX of [-1,2,3] is 0 while the MEX of [1,0,3] is 2.
Return the maximum MEX of nums after applying the mentioned operation any number of times.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,10,3], value = 6
›
Output:
0
💡 Note:
We have remainders [1,4,3] mod 6. To achieve MEX ≥ 1, we need to create value 0, which requires an element with remainder 0 mod 6. Since we don't have any element with remainder 0, the maximum MEX is 0.
Example 2 — Zero Value
$
Input:
nums = [3,4,1,9], value = 0
›
Output:
0
💡 Note:
When value=0, we cannot transform any elements. The array remains [3,4,1,9]. The MEX is 0 since 0 is not present.
Example 3 — Perfect Consecutive
$
Input:
nums = [0,1,4,5], value = 3
›
Output:
4
💡 Note:
We can transform: 0→0, 1→1, 4→1 (subtract 3), 5→2 (subtract 3). This gives remainders 0,1,1,2 mod 3. We can make consecutive sequence [0,1,2,3] by using the elements optimally.
Constraints
- 1 ≤ nums.length ≤ 105
- 0 ≤ nums[i] ≤ 109
- 1 ≤ value ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code