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

PythonServer Side ProgrammingProgramming

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

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 to [2, 3, 4] and [4, 5, 6, 7].

To solve this, we will follow these steps −

• counts := A map that contains elements of nums and its counts
• starts := a new list
• ends := a new list
• for each x in the items of the count in sorted order, do
• if count[x] > count[x - 1], then
• l := list of size (count[x] - count[x - 1]) and fill with x
• insert l into starts
• if count[x] > count[x + 1], then
• l := list of size (count[x] - count[x + 1]) and fill with x
• insert l into starts
• return true when all (start, end) pair satisfies (start + 2 <= end), otherwise return false

## Example (Python)

Let us see the following implementation to get better understanding −

Live Demo

from collections import Counter
class Solution:
def solve(self, nums):
count = Counter(nums)
starts = []
ends = []
for x in sorted(count):
if count[x] > count[x - 1]:
starts.extend([x] * (count[x] - count[x - 1]))
if count[x] > count[x + 1]:
ends.extend([x] * (count[x] - count[x + 1]))
return all(s + 2 <= e for s, e in zip(starts, ends))
ob = Solution()
nums = [2, 3, 4, 4, 5, 6, 7]
print(ob.solve(nums))

## Input

[6, 7, 5, 10, 13], 2

## Output

True