![]() |
VOOZH | about |
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:
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.
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.
true