Python3 Program to Find the K-th Largest Sum Contiguous Subarray
Last Updated :
05 Sep, 2024
Given an array of integers. Write a program to find the K-th largest sum of contiguous subarray within the array of numbers which has negative and positive numbers.
Examples:
Input: a[] = {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: a[] = {10, -10, 20, -40}
k = 6
Output: -10
Explanation: The 6th largest sum among
sum of all contiguous subarrays is -10.
A brute force approach is to store all the contiguous sums in another array and sort it and print the k-th largest. But in the case of the number of elements being large, the array in which we store the contiguous sums will run out of memory as the number of contiguous subarrays will be large (quadratic order)
An efficient approach is to store the pre-sum of the array in a sum[] array. We can find sum of contiguous subarray from index i to j as sum[j]-sum[i-1]
Now for storing the Kth largest sum, use a min heap (priority queue) in which we push the contiguous sums till we get K elements, once we have our K elements, check if the element is greater than the Kth element it is inserted to the min heap with popping out the top element in the min-heap, else not inserted. In the end, the top element in the min-heap will be your answer.
Below is the implementation of the above approach.
Python
# Python program to find the k-th largest sum
# of subarray
import heapq
# function to calculate kth largest element
# in contiguous subarray sum
def kthLargestSum(arr, n, k):
# array to store predix sums
sum = []
sum.append(0)
sum.append(arr[0])
for i in range(2, n + 1):
sum.append(sum[i - 1] + arr[i - 1])
# priority_queue of min heap
Q = []
heapq.heapify(Q)
# loop to calculate the contiguous subarray
# sum position-wise
for i in range(1, n + 1):
# loop to traverse all positions that
# form contiguous subarray
for j in range(i, n + 1):
x = sum[j] - sum[i - 1]
# if queue has less then k elements,
# then simply push it
if len(Q) < k:
heapq.heappush(Q, x)
else:
# it the min heap has equal to
# k elements then just check
# if the largest kth element is
# smaller than x then insert
# else its of no use
if Q[0] < x:
heapq.heappop(Q)
heapq.heappush(Q, x)
# the top element will be then kth
# largest element
return Q[0]
# Driver program to test above function
a = [10,-10,20,-40]
n = len(a)
k = 6
# calls the function to find out the
# k-th largest sum
print(kthLargestSum(a,n,k))
# This code is contributed by Kumar Suman
Output:
-10
Time complexity: O(n^2 log (k))
Auxiliary Space : O(k) for min-heap and we can store the sum array in the array itself as it is of no use. Please refer complete article on K-th Largest Sum Contiguous Subarray for more details!
Similar Reads
Python Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. Kadane's Algorithm: Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for
5 min read
K-th Largest Sum Contiguous Subarray 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 = 3Output: 14Explanation: All sum of contiguous subarrays are (20, 15, 14, -5, -6, -1), so t
15+ min read
Javascript Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Largest sum contiguous increasing subarray Given an array of n positive distinct integers. The problem is to find the largest sum of contiguous increasing subarray in O(n) time complexity. Examples : Input : arr[] = {2, 1, 4, 7, 3, 6}Output : 12Contiguous Increasing subarray {1, 4, 7} = 12Input : arr[] = {38, 7, 8, 10, 12}Output : 38Recommen
15 min read
Largest Sum Contiguous Subarray having unique elements Given an array arr[] of N positive integers, the task is to find the subarray having maximum sum among all subarrays having unique elements and print its sum. Input arr[] = {1, 2, 3, 3, 4, 5, 2, 1}Output: 15Explanation:The subarray having maximum sum with distinct element is {3, 4, 5, 2, 1}.Therefor
15+ min read
Largest sum contiguous subarray by adding S exactly at K different positions Given an array arr[] of length N, the task is to find the largest sum contiguous subarray by adding an integer S exactly at K different positions in the array for every K from [0, N]. Examples: Input: arr[] = {4, 1, 3, 2}, S = 2Output: 10 12 14 16 18Explanation:For k = 0, the sum of the array it sel
10 min read