2598. Smallest Missing Non-negative Integer After Operations Medium

@problem@discussion
#Array#Hash Table#Math#Greedy



1/**
2 * [2598] Smallest Missing Non-negative Integer After Operations
3 *
4 * You are given a 0-indexed integer array nums and an integer value.
5 * In one operation, you can add or subtract value from any element of nums.
6 * 
7 * 	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].
8 * 
9 * The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.
10 * 
11 * 	For example, the MEX of [-1,2,3] is 0 while the MEX of [1,0,3] is 2.
12 * 
13 * Return the maximum MEX of nums after applying the mentioned operation any number of times.
14 *  
15 * <strong class="example">Example 1:
16 * 
17 * Input: nums = [1,-10,7,13,6,8], value = 5
18 * Output: 4
19 * Explanation: One can achieve this result by applying the following operations:
20 * - Add value to nums[1] twice to make nums = [1,<u>0</u>,7,13,6,8]
21 * - Subtract value from nums[2] once to make nums = [1,0,<u>2</u>,13,6,8]
22 * - Subtract value from nums[3] twice to make nums = [1,0,2,<u>3</u>,6,8]
23 * The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
24 * 
25 * <strong class="example">Example 2:
26 * 
27 * Input: nums = [1,-10,7,13,6,8], value = 7
28 * Output: 2
29 * Explanation: One can achieve this result by applying the following operation:
30 * - subtract value from nums[2] once to make nums = [1,-10,<u>0</u>,13,6,8]
31 * The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
32 * 
33 *  
34 * Constraints:
35 * 
36 * 	1 <= nums.length, value <= 10^5
37 * 	-10^9 <= nums[i] <= 10^9
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/
43// discuss: https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn find_smallest_integer(nums: Vec<i32>, value: i32) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_2598() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.