VOOZH about

URL: https://www.geeksforgeeks.org/dsa/split-array-into-consecutive-subsequences-of-size-k-or-more/

⇱ Split Array into Consecutive Subsequences of Size K or more - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Split Array into Consecutive Subsequences of Size K or more

Last Updated : 18 Oct, 2025

Given an integer array arr[] that is sorted in increasing order and an integer k. Check whether it is possible to split arr[] 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 k or more.

Examples:

Input: arr[] = [2, 2, 3, 3, 4, 5], k = 2
Output: true
Explanation: arr can be split into three subsequence of length k - [2, 3], [2, 3], [4, 5].

Input: arr[] = [1, 1, 1, 1, 1], k = 4
Output: false
Explanation: It is impossible to split array into consecutive increasing subsequences of length 4 or more.

[Approach] Using MinHeap - O(n log(n)) Time and O(n) Space

The main idea is to use a min-heap and create a pair containing the element and its consecutive count, then push it into the min-heap. As we go through each element:

  • If the element is consecutive to the top element of the heap, we increase its consecutive count.
  • If it’s not consecutive, we check whether the top element’s count is at least k — if yes, we keep it; otherwise, return false.
  • We continue this process of adding pairs and updating counts until all elements are processed.
  • At the end, we ensure that all remaining subsequences in the heap have a length of at least k before returning true.

Output
true


Comment
Article Tags: