Split Array into Consecutive Subsequences LeetCode Solution

Last updated on October 5th, 2024 at 03:56 pm

Here, We see Split Array into Consecutive Subsequences LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Greedy, Heap

Companies

Google

Level of Question

Medium

Split Array into Consecutive Subsequences LeetCode Solution

Split Array into Consecutive Subsequences LeetCode Solution

Problem Statement

You are given an integer array nums that is sorted in non-decreasing order.

Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true:

  • Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer).
  • All subsequences have a length of 3 or more.

Return true if you can split nums according to the above conditions, or false otherwise.

subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., [1,3,5] is a subsequence of [1,2,3,4,5] while [1,3,2] is not).

Example 1:
Input: nums = [1,2,3,3,4,5]
Output: true
Explanation: nums can be split into the following subsequences: [1,2,3,3,4,5] –> 1, 2, 3 [1,2,3,3,4,5] –> 3, 4, 5

Example 2:
Input: nums = [1,2,3,3,4,4,5,5]
Output: true
Explanation: nums can be split into the following subsequences: [1,2,3,3,4,4,5,5] –> 1, 2, 3, 4, 5 [1,2,3,3,4,4,5,5] –> 3, 4, 5

Example 3:
Input: nums = [1,2,3,4,4,5]
Output: false
Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.

1. Split Array into Consecutive Subsequences LeetCode Solution C++

class Solution {
public:
    bool isPossible(vector<int>& nums) {
        unordered_map<int,int> mp;
        for(auto& it:nums)
            mp[it]++;
        for(auto& it:nums)
        {
            if(mp[it] == 0)
                continue;
            int freq = mp[it] , curr = it , count = 0;
            while(mp.count(curr) && mp[curr] >= freq)
            {
                freq = fmax(freq,mp[curr]);
                mp[curr]--;
                count++;
                curr++;
            }
            if(count < 3)
                return false;
        }
        return true;
    }
};

2. Split Array into Consecutive Subsequences LeetCode Solution Java

class Solution {
    public boolean isPossible(int[] nums) {
        HashMap<Integer,Integer> avaibilityMap = new HashMap<>();
        HashMap<Integer,Integer> wantMap = new HashMap<>();
        for(int i : nums){
            avaibilityMap.put(i, avaibilityMap.getOrDefault(i,0)+1);
        }
        for(int i=0;i<nums.length;i++){
            if(avaibilityMap.get(nums[i])<=0){
                continue;
            }
            else if(wantMap.getOrDefault(nums[i],0)>0){
                avaibilityMap.put(nums[i], avaibilityMap.getOrDefault(nums[i],0)-1);
                wantMap.put(nums[i], wantMap.getOrDefault(nums[i],0)-1);
                wantMap.put(nums[i]+1, wantMap.getOrDefault(nums[i]+1,0)+1);
            }
            else if(avaibilityMap.getOrDefault(nums[i],0)>0 && avaibilityMap.getOrDefault(nums[i]+1,0)>0 && avaibilityMap.getOrDefault(nums[i]+2,0)>0){
                avaibilityMap.put(nums[i], avaibilityMap.getOrDefault(nums[i],0)-1);
                avaibilityMap.put(nums[i]+1, avaibilityMap.getOrDefault(nums[i]+1,0)-1);
                avaibilityMap.put(nums[i]+2, avaibilityMap.getOrDefault(nums[i]+2,0)-1);
                wantMap.put(nums[i]+3, wantMap.getOrDefault(nums[i]+3,0)+1);
            }
            else{
                return false;
            }
        }
        return true;
    }
}

3. Split Array into Consecutive Subsequences LeetCode Solution JavaScript

var isPossible = function(nums) {
    if(nums.length < 3) return false;
    const freqMap = new Map(), appendfreq = new Map();
    nums.forEach(card => {
        if (!freqMap.has(card))
            freqMap.set(card, 0);
        
        freqMap.set(card, freqMap.get(card) + 1);
    });
     for (let i of nums) {
        if (freqMap.get(i) == 0) continue;
        else if ((appendfreq.get(i) || 0) > 0) {
            appendfreq.set(i, appendfreq.get(i) - 1);
            appendfreq.set(i+1, (appendfreq.get(i+1) || 0) + 1);
        }   
        else if ((freqMap.get(i+1) || 0) > 0 && (freqMap.get(i+2) || 0) > 0) {
            freqMap.set(i+1, freqMap.get(i+1) - 1);
            freqMap.set(i+2, freqMap.get(i+2) - 1);
            appendfreq.set(i+3, (appendfreq.get(i+3) || 0) + 1);
        }
        else return false;
        freqMap.set(i, freqMap.get(i) - 1);
    }
    return true;
};

4. Split Array into Consecutive Subsequences LeetCode Solution Python

class Solution(object):
    def isPossible(self, nums):
        seq = defaultdict(int)
        left = Counter(nums)
        for num in nums:
            if (not left[num]): continue
            if (seq[num-1] > 0):
                seq[num-1] -= 1 
                seq[num] += 1
                left[num] -= 1
            else:
                if (not left[num+1] or not left[num+2]):
                    return False
                left[num] -= 1
                left[num+1] -= 1
                left[num+2] -= 1
                seq[num+2] += 1
        return True
Scroll to Top