3041. Maximize Consecutive Elements in an Array After Modification
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```swift []
class Solution {
    func maxSelectedElements(_ nums: [Int]) -> Int {
        var nums = nums
        nums.sort()
        var maxLength = 1
        var lastNumber = nums[0]
        var normalStreak = 1
        var incrementedStreak = 1
        
        for current in nums.dropFirst() {
            let diff = current - lastNumber
            if diff == 0 {
                incrementedStreak = normalStreak + 1
            } else if diff == 1 {
                normalStreak += 1
                incrementedStreak += 1
            } else if diff == 2 {
                normalStreak = incrementedStreak + 1
                incrementedStreak = 1
            } else {
                normalStreak = 1
                incrementedStreak = 1
            }
            
            maxLength = max(maxLength, normalStreak, incrementedStreak)
            lastNumber = current
        }
        
        return maxLength
    }
}

```
['Swift']