Program to check whether we can split list into consecutive increasing sublists or not in Python

Suppose we have a list of numbers called nums that is sorted in non-decreasing order. We need to check whether it can be split into subsequences where each subsequence has a minimum length of 3 and contains consecutively increasing numbers.

So, if the input is like nums = [2, 3, 4, 4, 5, 6, 7], then the output will be True, as we can split the list into [2, 3, 4] and [4, 5, 6, 7].

Algorithm Approach

To solve this problem, we will follow these steps ?

  • counts ? A map that contains elements of nums and their frequency counts
  • starts ? A list to store starting points of subsequences
  • ends ? A list to store ending points of subsequences
  • For each element x in sorted order of counts:
    • If count[x] > count[x - 1], add new subsequence starts
    • If count[x] > count[x + 1], add subsequence ends
  • Return true when all (start, end) pairs satisfy start + 2 <= end, ensuring minimum length of 3

Implementation

Let us see the following implementation to get better understanding ?

from collections import Counter

class Solution:
    def solve(self, nums):
        count = Counter(nums)
        starts = []
        ends = []
        
        for x in sorted(count):
            # If current count is greater than previous, we need new subsequences
            if count[x] > count[x - 1]:
                starts.extend([x] * (count[x] - count[x - 1]))
            
            # If current count is greater than next, subsequences end here
            if count[x] > count[x + 1]:
                ends.extend([x] * (count[x] - count[x + 1]))
        
        # Check if each subsequence has minimum length 3
        return all(start + 2 <= end for start, end in zip(starts, ends))

# Test the solution
solution = Solution()
nums = [2, 3, 4, 4, 5, 6, 7]
result = solution.solve(nums)
print(f"Can split {nums} into consecutive increasing sublists: {result}")

The output of the above code is ?

Can split [2, 3, 4, 4, 5, 6, 7] into consecutive increasing sublists: True

How It Works

The algorithm works by analyzing the frequency of each number:

  • Count frequencies ? Counter({2: 1, 3: 1, 4: 2, 5: 1, 6: 1, 7: 1})
  • Find starts ? When count increases from previous number, new subsequences must start
  • Find ends ? When count decreases to next number, subsequences must end
  • Validate length ? Each subsequence must span at least 3 consecutive numbers

Additional Example

# Test with a case that returns False
solution = Solution()
nums_false = [1, 2, 3, 3, 4, 5]
result_false = solution.solve(nums_false)
print(f"Can split {nums_false}: {result_false}")

# Test with another valid case
nums_valid = [1, 2, 3, 3, 4, 4, 5, 5, 6]
result_valid = solution.solve(nums_valid)
print(f"Can split {nums_valid}: {result_valid}")
Can split [1, 2, 3, 3, 4, 5]: True
Can split [1, 2, 3, 3, 4, 4, 5, 5, 6]: True

Conclusion

This algorithm efficiently determines if a sorted list can be split into consecutive increasing sublists of minimum length 3. It uses frequency analysis to identify subsequence boundaries and validates the minimum length constraint.

Updated on: 2026-03-25T13:35:05+05:30

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements