Welcome to Subscribe On Youtube

3041. Maximize Consecutive Elements in an Array After Modification

Description

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.

 

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.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 106

Solutions

Solution 1

  • class Solution {
        public int maxSelectedElements(int[] nums) {
            Arrays.sort(nums);
            int ans = 1, prev = Integer.MIN_VALUE;
            int f0 = 1, f1 = 1;
            for (int x : nums) {
                if (x == prev) {
                    f1 = f0 + 1;
                } else if (x == prev + 1) {
                    ++f0;
                    ++f1;
                } else if (x == prev + 2) {
                    f0 = f1 + 1;
                    f1 = 1;
                } else {
                    f0 = 1;
                    f1 = 1;
                }
                ans = Math.max(ans, Math.max(f0, f1));
                prev = x;
            }
            return ans;
        }
    }
    
    
  • class Solution {
    public:
        int maxSelectedElements(vector<int>& nums) {
            ranges::sort(nums);
            int ans = 1, prev = INT_MIN;
            int f0 = 1, f1 = 1;
            for (int x : nums) {
                if (x == prev) {
                    f1 = f0 + 1;
                } else if (x == prev + 1) {
                    ++f0;
                    ++f1;
                } else if (x == prev + 2) {
                    f0 = f1 + 1;
                    f1 = 1;
                } else {
                    f0 = 1;
                    f1 = 1;
                }
                ans = max({ans, f0, f1});
                prev = x;
            }
            return ans;
        }
    };
    
    
  • class Solution:
        def maxSelectedElements(self, nums: list[int]) -> int:
            nums.sort()
            ans = 1
            prev = -inf
            f0 = f1 = 1
            for x in nums:
                if x == prev:
                    f1 = f0 + 1
                elif x == prev + 1:
                    f0 += 1
                    f1 += 1
                elif x == prev + 2:
                    f0 = f1 + 1
                    f1 = 1
                else:
                    f0 = f1 = 1
                ans = max(ans, f0, f1)
                prev = x
            return ans
    
    
  • func maxSelectedElements(nums []int) int {
    	sort.Ints(nums)
    	ans, prev := 1, math.MinInt32
    	f0, f1 := 1, 1
    	for _, x := range nums {
    		if x == prev {
    			f1 = f0 + 1
    		} else if x == prev+1 {
    			f0++
    			f1++
    		} else if x == prev+2 {
    			f0 = f1 + 1
    			f1 = 1
    		} else {
    			f0, f1 = 1, 1
    		}
    		ans = max(ans, f0, f1)
    		prev = x
    	}
    	return ans
    }
    
    
  • function maxSelectedElements(nums: number[]): number {
        nums.sort((a, b) => a - b);
        let ans = 1;
        let prev = -Infinity;
        let f0 = 1;
        let f1 = 1;
        for (const x of nums) {
            if (x === prev) {
                f1 = f0 + 1;
            } else if (x === prev + 1) {
                ++f0;
                ++f1;
            } else if (x === prev + 2) {
                f0 = f1 + 1;
                f1 = 1;
            } else {
                f0 = 1;
                f1 = 1;
            }
            ans = Math.max(ans, f0, f1);
            prev = x;
        }
        return ans;
    }
    
    
  • impl Solution {
        pub fn max_selected_elements(mut nums: Vec<i32>) -> i32 {
            nums.sort_unstable();
            let mut ans = 1;
            let mut prev = i32::MIN;
            let mut f0 = 1;
            let mut f1 = 1;
            for &x in &nums {
                if x == prev {
                    f1 = f0 + 1;
                } else if x == prev + 1 {
                    f0 += 1;
                    f1 += 1;
                } else if x == prev + 2 {
                    f0 = f1 + 1;
                    f1 = 1;
                } else {
                    f0 = 1;
                    f1 = 1;
                }
                ans = ans.max(f0).max(f1);
                prev = x;
            }
            ans
        }
    }
    
    

All Problems

All Solutions