Maximize Consecutive Elements in an Array After Modification - Problem

You're given an array of positive integers, and you have a special power: you can increase any element by at most 1. Your goal is to create the longest possible consecutive sequence by selecting elements from your modified array.

For example, if you have [2, 3, 5], you could increase the last element to get [2, 3, 6], but you still can't form a consecutive sequence longer than 2 elements. However, if you increase it to 4, you get [2, 3, 4] - a perfect consecutive sequence of length 3!

Consecutive means: Elements that follow each other in order when sorted, like [3, 4, 5]

Not consecutive: [3, 4, 6] ❌ (gap between 4 and 6), [1, 1, 2, 3] ❌ (duplicates)

Return the maximum number of elements you can select to form a consecutive sequence.

Input & Output

example_1.py — Basic Case
$ Input: nums = [2, 3, 5, 4]
Output: 4
💡 Note: We can use all elements: keep 2, 3, 4 as they are, and increment 5 to 6. This gives us the consecutive sequence [2, 3, 4, 5] with length 4.
example_2.py — Increment Strategy
$ Input: nums = [1, 3, 2, 1]
Output: 4
💡 Note: Increment one of the 1s to 2, and increment the other 1 to get another value. We can form [1, 2, 3, 4] by using the two 1s (one as 1, one incremented to 2), the 2 (incremented to 3), and the 3 (incremented to 4).
example_3.py — Optimal Selection
$ Input: nums = [1, 5, 51, 50, 100]
Output: 3
💡 Note: The best consecutive sequence we can form is [49, 50, 51] by using: 51 as is, 50 as is, and increment 50-1=49 (but we don't have 49, so we increment 50 to 51... wait, let's reconsider: we can form [50, 51, 52] using 50, 51, and increment 51 to 52. Actually, the optimal is [1, 2, 3] using 1, increment 1 to 2, increment 5 to 2 (no, that's wrong). Let me recalculate: we can get [50, 51, 52] with length 3.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 106
  • Each element can be increased by at most 1
  • The resulting consecutive sequence must have distinct elements

Visualization

Tap to expand
Building the Consecutive BridgeInput Stones: [3, 4, 6, 7, 6]34676Frequency Count:Stone 3: 1 piece (can be 3 or 4)Stone 4: 1 piece (can be 4 or 5)Stone 6: 2 pieces (can be 6 or 7)Build Bridge Starting from 4:45678Length: 5!Sources: 4(from 3+1,4), 5(from 4+1), 6(from 6), 7(from 7,6+1), 8(from 6+1)🎯 Key Insight: Each stone can contribute to two possible bridge positions!
Understanding the Visualization
1
Survey Your Stones
Count how many stones of each number you have. Each stone can either stay at its value or be upgraded by +1.
2
Try Each Starting Point
For each possible starting number, try to build the longest consecutive bridge from that point.
3
Greedy Bridge Building
At each position, use available stones optimally. Prefer using stones at their original value when possible.
4
Find the Longest Bridge
Compare all possible starting points and return the longest consecutive sequence found.
Key Takeaway
🎯 Key Insight: The problem becomes finding the longest consecutive sequence where each position can be filled by elements from at most two source values (original or decremented by 1).
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
38.2K Views
High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen