Skip to content

Practice Maximize Consecutive Elements in an Array After Modification

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3041
LeetCode Problem

Maximize Consecutive Elements in an Array After Modification

Problem Statement

You are given a 0-indexed array nums consisting of positive integers. Initially, you can increase the value of any element in the array by at most 1. 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. Return the maximum number of elements that you can select.

Source: Maximize Consecutive Elements in an Array After Modification on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,1,5,1,1]
Output
3

# Example 2

Input
nums = [1,4,7,10]
Output
1

Constraints

1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6

Related LeetCode Problems

Practice more problems similar to Maximize Consecutive Elements in an Array After Modification.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.