3041. Maximize Consecutive Elements in an Array After Modification Hard

@problem@discussion
#Array#Dynamic Programming#Sorting



1/**
2 * [3041] Maximize Consecutive Elements in an Array After Modification
3 *
4 * You are given a 0-indexed array nums consisting of positive integers.
5 * Initially, you can increase the value of any element in the array by at most 1.
6 * After that, you need to select one or more elements from the final array such that those elements are consecutive when sorted in increasing order. For example, the elements [3, 4, 5] are consecutive while [3, 4, 6] and [1, 1, 2, 3] are not.<!-- notionvc: 312f8c5d-40d0-4cd1-96cc-9e96a846735b -->
7 * Return the maximum number of elements that you can select.
8 *  
9 * <strong class="example">Example 1:
10 * 
11 * Input: nums = [2,1,5,1,1]
12 * Output: 3
13 * Explanation: We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
14 * We select the elements [<u>3</u>,<u>1</u>,5,<u>2</u>,1] and we sort them to obtain [1,2,3], which are consecutive.
15 * It can be shown that we cannot select more than 3 consecutive elements.
16 * <strong class="example">Example 2:
17 * 
18 * Input: nums = [1,4,7,10]
19 * Output: 1
20 * Explanation: The maximum consecutive elements that we can select is 1.
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	1 <= nums.length <= 10^5
26 * 	1 <= nums[i] <= 10^6
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/maximize-consecutive-elements-in-an-array-after-modification/
32// discuss: https://leetcode.com/problems/maximize-consecutive-elements-in-an-array-after-modification/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn max_selected_elements(nums: Vec<i32>) -> i32 {
38        0
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_3041() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.