#3041
Maximize Consecutive Elements in an Array After Modification
master · 1890 · lc hard +32 · verified · 33.6% accepted · 178 likes · top 11%
Description
You are given a 0-indexed array nums of positive integers.
You may initially increase any element by at most 1.
Then select one or more elements from the resulting array such that, when sorted, they form a consecutive sequence (e.g., [3, 4, 5] is consecutive; [3, 4, 6] is not).
Return the maximum number of elements you can select.
Example 1:
Input: nums = [2,1,5,1,1]
Output: 3
Explanation: We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
We select the elements [3,1,5,2,1] and we sort them to obtain [1,2,3], which are consecutive.
It can be shown that we cannot select more than 3 consecutive elements.
Example 2:
Input: nums = [1,4,7,10]
Output: 1
Explanation: The maximum consecutive elements that we can select is 1.
Code
1
2
3