VOOZH about

URL: https://www.geeksforgeeks.org/dsa/k-th-largest-sum-contiguous-subarray/

⇱ K-th Largest Sum Contiguous Subarray - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

K-th Largest Sum Contiguous Subarray

Last Updated : 8 May, 2025

Given an array arr[] of size n, the task is to find the kth largest sum of contiguous subarray within the array of numbers that has both negative and positive numbers.

Examples:

Input: arr[] = [20, -5, -1], k = 3
Output: 14
Explanation: All sum of contiguous subarrays are (20, 15, 14, -5, -6, -1), so the 3rd largest sum is 14.

Input: arr[] = [10, -10, 20, -40], k = 6
Output: -10
Explanation: The 6th largest sum among sum of all contiguous subarrays is -10.

[Naive Approach - 1] Using Sorting - O(n2 * log n) Time and O(n ^ 2) Space

The idea is to calculate the Kth largest sum of contiguous subarrays by generating all possible contiguous subarray sums, storing them, and then sorting them in decreasing order to directly access the Kth largest. However, this approach can lead to memory issues when the input array size is large, as the number of contiguous subarrays grows quadratically with the size of the input array.

Step-by-Step Implementation:

  • Traverse the input array using two nested loops to generate all contiguous subarrays.
  • For each subarray, calculate the sum and store it in a list.
  • After generating all subarray sums, sort the list in descending order.
  • Return the element at the (k - 1)th index from the sorted list as the Kth largest sum.

Output
14

[Naive Approach - 2] Using Prefix Sum and Sorting - O(n2 * log n) Time and O(n ^ 2) Space

The idea is to leverage a prefix-sum array to compute every contiguous subarray sum in constant time per pair of endpoints, collect all those sums into a single list, sort that list in descending order, and then directly pick out the Kth largest value.

Step-by-Step Implementation:

  • Build a prefix-sum array prefixSum of length n+1 where prefixSum[i] equals the total of the first i elements.
  • Initialize an empty list to hold all subarray sums.
  • For each start index i from 1 to n, and for each end index j from i to n, compute the subarray sum as prefixSum[j] – prefixSum[i–1] and append it to the list.
  • Sort the list of subarray sums in decreasing order.
  • Return the element at index k–1 in the sorted list as the Kth largest sum.

Output
14

[Expected Approach] - Using Min Heap - O(n2 * log k) Time and O(k) Space

The key idea is to store the pre-sum of the array in a sum[] array. One can find the sum of contiguous subarray from index i to j as sum[j] - sum[i-1]. After this step, this problem becomes same as k-th smallest element in an array. So we generate all possible contiguous subarray sums and push them into the Min-Heap only if the size of Min-Heap is less than K or the current sum is greater than the root of the Min-Heap. In the end, the root of the Min-Heap is the required answer.

Follow the given steps to solve the problem using the above approach:

  • Create a prefix sum array of the input array
  • Create a Min-Heap that stores the subarray sum
  • Iterate over the given array using the variable i such that 1 <= i <= N, here i denotes the starting point of the subarray
    • Create a nested loop inside this loop using a variable j such that i <= j <= N, here j denotes the ending point of the subarray
      • Calculate the sum of the current subarray represented by i and j, using the prefix sum array
      • If the size of the Min-Heap is less than K, then push this sum into the heap
      • Otherwise, if the current sum is greater than the root of the Min-Heap, then pop out the root and push the current sum into the Min-Heap
  • Now the root of the Min-Heap denotes the Kth largest sum, Return it

Output
14
Comment