You are not logged in. Please login at to post your questions!


KSUBSUM - Editorial







Given an array A of N integers, we are asked to find the Kth maximum sum of a contiguous subarray of elements. Finding the Maximum sum of a subarray is a well known problem. If the array elements are all non-negative, we can use binary search to find the answer in O(n log S) time, where S is the maximum sum of a subarray. In this problem, the values can be negative. As with the other problems in this set, look at the constraints carefully, N ≤ 10,000 and K ≤ 2012. We go through the array from left to right and at each index i, we find all K maximum sums of subarrays, ending at index i. If S is the prefix sum array, where S[i] = A[1] + A[2] + ... + A[i], then all subarray sums ending at index i can be computed using S[i] - S[j], for j = 0 to i-1 and considering S[0] = 0. But we only need top K of them, so we can subtract S[j] s in non-decreasing order and only K of them. This requires us to maintain the array S in sorted order and this can be done similar to insertion sort in O(K) time per insertion.

Now that we have the top-K subarray sums ending at index i, we can compare them with the current top-K best answers so far and pick some of them and drop others. Note that at each step you only need to maintain K minimum prefix sums and K maximum subarray sums so far. Given the best K sums so far and the current K sums, we can merge the two sorted arrays and get the updated best K sums. This can also be done in O(K) time. The overall time complexity is O(NK). Maintaining a set ( or heap ) in which each insertion is additional O(log K) only increases the running time by more than 10x and may not fit in the given time limit.


Can be found here.


Can be found here.

This question is marked "community wiki".

asked 22 Nov '12, 12:09

admin's gravatar image

0★admin ♦♦
accept rate: 36%

@shubham2892 : The number of sub arrays is N(N-1)/2 which is O(N^2) . k1 , k2 , k3 are bounded in this problem . But the total number of sub arrays are not and they can be a huge number like 50000000 for N = 10000 . You will have count variable going up to that . And you are making array access with count variable . That's the cause of runtime error .


answered 01 Feb '13, 11:48

vineetpaliwal's gravatar image

accept rate: 12%

edited 01 Feb '13, 11:55

Using priority queue :


answered 23 Jun '16, 16:36

beginner007's gravatar image

accept rate: 0%

Lets consider the 3rd test case

Given array is 20 -15 10 -15

so cumulativeSum array becomes 20 5 15 0

now till each position of array you will have certain possible sums

1 -> 20

2 -> 5,-15

3 -> 15,-5,0

4 -> 0,-20,-5,-15

Now in your first approach you are putting these elements in the multiset row wise like 20 then 5 and -15 and so on.

And in your second approach you are putting these elements in the multiset col wise like 20,5,15,0 and then -15,-5,-29 and so on.

This making your complexity same but as the traversal is different so the number of times the multiset will be updated is variable.

I see that as the only reason for one getting accepted and other getting tle.

So O(n^2 logn) won't pass always as stated in the editorial too.


answered 26 Jul '16, 15:07

dodobhoot's gravatar image

accept rate: 25%

i am getting runtime error..please help..


answered 01 Feb '13, 01:32

shubham2892's gravatar image

accept rate: 0%

Answer is hidden as author is suspended. Click here to view.

answered 31 May '15, 10:00

vsanjay's gravatar image

accept rate: 10%

@admin: Can you explain how would you do this-

If the array elements are all non-negative, we can use binary search to find the answer in O(n log S) time, where S is the maximum sum of a subarray.


answered 05 Jan '16, 21:36

disisbig's gravatar image

accept rate: 0%

edited 05 Jan '16, 21:36

I solved this question using Multiset.But, still there is some doubt. AC solution: TLE solution:

Both of them is having complexity O(n^2logn).Why one is getting TLE with slight change in approach?


answered 26 Jul '16, 04:57

rohit_0801's gravatar image

accept rate: 10%

Still, i guess the test cases suited my AC approach, because by reversing inner loop we can no way guarantee optimization.Its just sheer luck that my solution passed in O(n^2 logn). :)


answered 26 Jul '16, 16:28

rohit_0801's gravatar image

accept rate: 10%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 22 Nov '12, 12:09

question was seen: 8,637 times

last updated: 26 Jul '16, 16:28