You are given an integer array nums. You may perform the following operation on any element any number of times: reduce nums[i] to any integer in the range [0, nums[i]].
The MEX (minimum excludant) of an array is the smallest non-negative integer that is not present in the array.
Return the maximum possible MEX of the array after performing the operations optimally.
Function Signature
def maximize_mex(nums: list[int]) -> int:
Rules
-
An element can be left unchanged, and it can never be increased. After the operations, element
i
holds some integer from 0 to its original value.
-
Every element stays in the array. You cannot delete elements.
Constraints
-
1 <= nums.length <= 10^5
-
0 <= nums[i] <= 10^9
Examples
Input: nums = [3, 0, 1, 1, 5]
Output: 4
Sorted, the array is [0, 1, 1, 3, 5]. Keep 0 and the first 1, reduce 3 to 2 and 5 to 3, and the array becomes [0, 1, 1, 2, 3] with MEX 4. Reaching 5 would need two elements that are at least 3, and only 3 and 5 qualify, but one of them is also needed for 2.
Input: nums = [0, 0, 0]
Output: 1
The value 0 is present, and no element can become 1 because none can be increased.
Input: nums = [1, 1, 1, 7]
Output: 3
Two of the 1s cover 0 and 1, and 7 is reduced to 2. The third 1 can only repeat 0 or 1, and no element is left to cover 3.