2598. Smallest Missing Non-negative Integer After Operations

Medium

220

20

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*.

**Example 1:**

Input:nums = [1,-10,7,13,6,8], value = 5Output:4Explanation:One can achieve this result by applying the following operations: - Add value to nums[1] twice to make nums = [1,,7,13,6,8] - Subtract value from nums[2] once to make nums = [1,0,0,13,6,8] - Subtract value from nums[3] twice to make nums = [1,0,2,2,6,8] The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.3

**Example 2:**

Input:nums = [1,-10,7,13,6,8], value = 7Output:2Explanation:One can achieve this result by applying the following operation: - subtract value from nums[2] once to make nums = [1,-10,,13,6,8] The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.0

**Constraints:**

`1 <= nums.length, value <= 10`

^{5}`-10`

^{9}<= nums[i] <= 10^{9}

Accepted

9.3K

Submissions

24.1K

Acceptance Rate

38.6%

Seen this question in a real interview before?

1/4

Yes

No

Discussion (0)

Related Topics

Copyright ©️ 2023 LeetCode All rights reserved