You can find the problem here: https://www.hackerrank.com/contests/lambda-calculi-oct14/challenges/order-exercises
You are given an array of integers, along with the following definitions:
- a subarray is a contiguous segment of an array. For example is a subarray, where
- We say that a sum of a subarray is a sum of elements in this subarray
- We say that subarray is greater than subarray if and only if one of the following statements is true:
- has a greater sum than
- and has the same sum and begins earlier
- and has the same sum, they start in the same place and the length of is smaller than the length of
It is guaranteed that there is no 0 in the array .
You are given also an integer . The task is to print as many as possible, but not more than , subarrays with a positive sum in the following order.
- The first subarray is the greatest subarray in the array according to above definition.
- The subarray is the greatest subarray disjoint to any of the subarray, where (disjoint means that they have no common elements).
If you are familiar with the Maximum subarray problem you may notice that our problem is a natural extension of it.
Simple, but not enough solution
Probably the first solution which comes to mind is to run Kadane algorithm or similar to get the value of the greatest subarray, then assign to every element of that subarray, find the second greatest subarray and so on. We iterate that process while there exists a subarray with a positive sum.
The time complexity of this method is which gives you some points, but it is too slow to pass all testcases even if it is hardly optimized.
The general pattern of above solution is good, but we have to find the next subarray faster. A segment tree can help us here.
The first thing here is to implement a segment tree which supports the following query in time:
returns the greatest subarray in
Let be a subtree represented by a node and be an array corresponding to elements covered by . We can implement that query maintaining in every node several values:
- sum of the greatest subarray in
- greatest prefix sum of
- greatest suffix sum of
- sum of values of elements in
You can compute these values of any based on values in its children. Values for leaves are obvious. After that, if you are familiar with segment trees, you can implement the easily.
In order to do the next step we need a priority queue, let's call it . We store in the queue triplets where:
- is a range in which we search for subarrays
- is the value of the greatest subarray in R
- represents indices of the greatest subarray in R
Ordering of elements in is determined by values and .
First we compute the value and indices of the greatest subarray in the whole array. We do it using . Let's assume that it is and has a sum . We put into the queue a triplet .
While is not empty and we have found less than subarrays so far, we select the greatest triplet from the queue and remove it from there. Let's assume that the triplet is . We print as a result and if a range is not empty, we compute and if it returns a subarray with a positive sum with indices , we put into the queue a triplet . We do the same thing with a range .
Time needed to construct the tree is and we do at most queries, so the total complexity is , because .