Program to split lists into strictly increasing sublists of size greater than k in Python

PythonServer Side ProgrammingProgramming

Suppose we have a list of numbers called nums, and another value k, we have to check whether it is possible to split the list into sublists lists such that each sublist has length ≥ k and that is strictly increasing. The list does not need to be split contiguously.

So, if the input is like nums = [6, 7, 5, 10, 13] k = 2, then the output will be True, as the split is [5, 6] and [7, 10, 13].

To solve this, we will follow these steps −

  • c := A map that contains elements of nums and its counts
  • max_count := maximum of all frequencies of c
  • return True when max_count * k <= size of nums otherwise 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, k):
      c = Counter(nums)
      max_count = max([v for k, v in c.items()])
      return max_count * k <= len(nums)
ob = Solution()
nums = [6, 7, 5, 10, 13]
k = 2
print(ob.solve(nums, k))


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


Updated on 12-Dec-2020 10:27:46