3397. Maximum Number of Distinct Elements After Operations Medium

@problem@discussion
#Array#Greedy#Sorting



1/**
2 * [3397] Maximum Number of Distinct Elements After Operations
3 *
4 * You are given an integer array nums and an integer k.
5 * You are allowed to perform the following operation on each element of the array at most once:
6 * 
7 * 	Add an integer in the range [-k, k] to the element.
8 * 
9 * Return the maximum possible number of distinct elements in nums after performing the operations.
10 *  
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">nums = [1,2,2,3,3,4], k = 2</span>
14 * Output: <span class="example-io">6</span>
15 * Explanation:
16 * nums changes to [-1, 0, 1, 2, 3, 4] after performing operations on the first four elements.
17 * </div>
18 * <strong class="example">Example 2:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [4,4,4,4], k = 1</span>
21 * Output: <span class="example-io">3</span>
22 * Explanation:
23 * By adding -1 to nums[0] and 1 to nums[1], nums changes to [3, 5, 4, 4].
24 * </div>
25 *  
26 * Constraints:
27 * 
28 * 	1 <= nums.length <= 10^5
29 * 	1 <= nums[i] <= 10^9
30 * 	0 <= k <= 10^9
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/maximum-number-of-distinct-elements-after-operations/
36// discuss: https://leetcode.com/problems/maximum-number-of-distinct-elements-after-operations/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn max_distinct_elements(nums: Vec<i32>, k: i32) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_3397() {
54    }
55}
56


Back
© 2025 bowen.ge All Rights Reserved.