#2598
Smallest Missing Non-negative Integer After Operations
specialist · 800 · lc medium +31 · premium · verified · 55.9% accepted · 802 likes · top 49%
Description
You are given a 0-indexed integer array nums and an integer value. In each operation, add or subtract value from any element. The MEX of an array is the smallest non-negative integer not in it. Return the maximum MEX achievable after any number of operations.
Example 1:
Input: nums = [1,-10,7,13,6,8], value = 5
Output: 4
Explanation: One can achieve this result by applying the following operations:
- Add value to nums[1] twice to make nums = [1,0,7,13,6,8]
- Subtract value from nums[2] once to make nums = [1,0,2,13,6,8]
- Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8]
The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
Example 2:
Input: nums = [1,-10,7,13,6,8], value = 7
Output: 2
Explanation: One can achieve this result by applying the following operation:
- subtract value from nums[2] once to make nums = [1,-10,0,13,6,8]
The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
Code
1
2
3