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
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