K maximum sums of non-overlapping contiguous sub-arrays
Last Updated :
29 Mar, 2024
Given an Array of Integers and an Integer value k, find out k non-overlapping sub-arrays which have k maximum sums.
Examples:
Input : arr1[] = {4, 1, 1, -1, -3, -5, 6, 2, -6, -2},
k = 3.
Output : {4,1},{1} and {6,2} can be taken, thus the output should be 14.
Input : arr2 = {5, 1, 2, -6, 2, -1, 3, 1},
k = 2.
Output : Maximum non-overlapping sub-array sum1: 8,
starting index: 0, ending index: 2.
Maximum non-overlapping sub-array sum2: 5,
starting index: 4, ending index: 7.
Prerequisite: Kadane's Algorithm
Kadane's algorithm finds out only the maximum subarray sum, but using the same algorithm we can find out k maximum non-overlapping subarray sums. The approach is:
- Find out the maximum subarray in the array using Kadane's algorithm. Also find out its starting and end indices. Print the sum of this subarray.
- Fill each cell of this subarray by -infinity.
- Repeat process 1 and 2 for k times.
C++
// C++ program to find out k maximum
// non-overlapping sub-array sums.
#include <bits/stdc++.h>
using namespace std;
// Function to compute k maximum
// sub-array sums.
void kmax(int arr[], int k, int n) {
// In each iteration it will give
// the ith maximum subarray sum.
for(int c = 0; c < k; c++){
// Kadane's algorithm.
int max_so_far = numeric_limits<int>::min();
int max_here = 0;
// compute starting and ending
// index of each of the sub-array.
int start = 0, end = 0, s = 0;
for(int i = 0; i < n; i++)
{
max_here += arr[i];
if (max_so_far < max_here)
{
max_so_far = max_here;
start = s;
end = i;
}
if (max_here < 0)
{
max_here = 0;
s = i + 1;
}
}
// Print out the result.
cout << "Maximum non-overlapping sub-array sum"
<< (c + 1) << ": "<< max_so_far
<< ", starting index: " << start
<< ", ending index: " << end << "." << endl;
// Replace all elements of the maximum subarray
// by -infinity. Hence these places cannot form
// maximum sum subarray again.
for (int l = start; l <= end; l++)
arr[l] = numeric_limits<int>::min();
}
cout << endl;
}
// Driver Program
int main()
{
// Test case 1
int arr1[] = {4, 1, 1, -1, -3,
-5, 6, 2, -6, -2};
int k1 = 3;
int n1 = sizeof(arr1) / sizeof(arr1[0]);
// Function calling
kmax(arr1, k1, n1);
// Test case 2
int arr2[] = {5, 1, 2, -6, 2, -1, 3, 1};
int k2 = 2;
int n2 = sizeof(arr2)/sizeof(arr2[0]);
// Function calling
kmax(arr2, k2, n2);
return 0;
}
Java
// Java program to find out k maximum
// non-overlapping sub-array sums.
class GFG {
// Method to compute k maximum
// sub-array sums.
static void kmax(int arr[], int k, int n) {
// In each iteration it will give
// the ith maximum subarray sum.
for(int c = 0; c < k; c++)
{
// Kadane's algorithm.
int max_so_far = Integer.MIN_VALUE;
int max_here = 0;
// compute starting and ending
// index of each of the sub-array.
int start = 0, end = 0, s = 0;
for(int i = 0; i < n; i++)
{
max_here += arr[i];
if (max_so_far < max_here)
{
max_so_far = max_here;
start = s;
end = i;
}
if (max_here < 0)
{
max_here = 0;
s = i + 1;
}
}
// Print out the result.
System.out.println("Maximum non-overlapping sub-arraysum" +
(c + 1) + ": " + max_so_far +
", starting index: " + start +
", ending index: " + end + ".");
// Replace all elements of the maximum subarray
// by -infinity. Hence these places cannot form
// maximum sum subarray again.
for (int l = start; l <= end; l++)
arr[l] = Integer.MIN_VALUE;
}
System.out.println();
}
// Driver Program
public static void main(String[] args)
{
// Test case 1
int arr1[] = {4, 1, 1, -1, -3, -5,
6, 2, -6, -2};
int k1 = 3;
int n1 = arr1.length;
// Function calling
kmax(arr1, k1, n1);
// Test case 2
int arr2[] = {5, 1, 2, -6, 2, -1, 3, 1};
int k2 = 2;
int n2 = arr2.length;
// Function calling
kmax(arr2, k2, n2);
}
}
// This code is contributed by Nirmal Patel
Python3
# Python program to find out k maximum
# non-overlapping subarray sums.
# Function to compute k
# maximum sub-array sums.
def kmax(arr, k, n):
# In each iteration it will give
# the ith maximum subarray sum.
for c in range(k):
# Kadane's algorithm
max_so_far = -float("inf")
max_here = 0
# compute starting and ending
# index of each of the subarray
start = 0
end = 0
s = 0
for i in range(n):
max_here += arr[i]
if (max_so_far < max_here):
max_so_far = max_here
start = s
end = i
if (max_here < 0):
max_here = 0
s = i + 1
# Print out the result
print("Maximum non-overlapping sub-array sum",
c + 1, ": ", max_so_far, ", starting index: ",
start, ", ending index: ", end, ".", sep = "")
# Replace all elements of the maximum subarray
# by -infinity. Hence these places cannot form
# maximum sum subarray again.
for l in range(start, end+1):
arr[l] = -float("inf")
print()
# Driver Program
# Test case 1
arr1 = [4, 1, 1, -1, -3, -5, 6, 2, -6, -2]
k1 = 3
n1 = len(arr1)
# Function calling
kmax(arr1, k1, n1)
# Test case 2
arr2 = [5, 1, 2, -6, 2, -1, 3, 1]
k2 = 2
n2 = len(arr2)
# Function calling
kmax(arr2, k2, n2)
C#
// C# program to find out k maximum
// non-overlapping sub-array sums.
using System;
class GFG {
// Method to compute k
// maximum sub-array sums.
static void kmax(int []arr, int k, int n) {
// In each iteration it will give
// the ith maximum subarray sum.
for(int c = 0; c < k; c++)
{
// Kadane's algorithm.
int max_so_far = int.MinValue;
int max_here = 0;
// compute starting and ending
// index of each of the sub-array.
int start = 0, end = 0, s = 0;
for(int i = 0; i < n; i++)
{
max_here += arr[i];
if (max_so_far < max_here)
{
max_so_far = max_here;
start = s;
end = i;
}
if (max_here < 0)
{
max_here = 0;
s = i + 1;
}
}
// Print out the result.
Console.WriteLine("Maximum non-overlapping sub-arraysum" +
(c + 1) + ": "+ max_so_far +
", starting index: " + start +
", ending index: " + end + ".");
// Replace all elements of the maximum subarray
// by -infinity. Hence these places cannot form
// maximum sum subarray again.
for (int l = start; l <= end; l++)
arr[l] = int.MinValue;
}
Console.WriteLine();
}
// Driver Program
public static void Main(String[] args)
{
// Test case 1
int []arr1 = {4, 1, 1, -1, -3, -5,
6, 2, -6, -2};
int k1 = 3;
int n1 = arr1.Length;
// Function calling
kmax(arr1, k1, n1);
// Test case 2
int []arr2 = {5, 1, 2, -6, 2, -1, 3, 1};
int k2 = 2;
int n2 = arr2.Length;
// Function calling
kmax(arr2, k2, n2);
}
}
// This code is contributed by parashar...
PHP
<?php
// PHP program to find out k maximum
// non-overlapping sub-array sums.
// Method to compute k
// maximum sub-array sums.
function kmax($arr, $k, $n) {
// In each iteration it will give
// the ith maximum subarray sum.
for( $c = 0; $c < $k; $c++)
{
// Kadane's algorithm.
$max_so_far = PHP_INT_MIN;
$max_here = 0;
// compute starting and ending
// index of each of the sub-array.
$start = 0; $end = 0; $s = 0;
for($i = 0; $i < $n; $i++)
{
$max_here += $arr[$i];
if ($max_so_far < $max_here)
{
$max_so_far = $max_here;
$start = $s;
$end = $i;
}
if ($max_here < 0)
{
$max_here = 0;
$s = $i + 1;
}
}
// Print out the result.
echo "Maximum non-overlapping sub-arraysum" ;
echo ($c + 1) , ": ", $max_so_far ;
echo", starting index: " , $start ;
echo", ending index: " , $end , ".";
echo"\n";
// Replace all elements of the maximum subarray
// by -infinity. Hence these places cannot form
// maximum sum subarray again.
for ( $l = $start; $l <= $end; $l++)
$arr[$l] = PHP_INT_MIN;
}
echo "\n";
}
// Driver Program
// Test case 1
$arr1 = array(4, 1, 1, -1, -3, -5,
6, 2, -6, -2);
$k1 = 3;
$n1 = count($arr1);
// Function calling
kmax($arr1, $k1, $n1);
// Test case 2
$arr2 = array(5, 1, 2, -6, 2, -1, 3, 1);
$k2 = 2;
$n2 =count($arr2);
// Function calling
kmax($arr2, $k2, $n2);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// JavaScript program to find out k maximum
// non-overlapping sub-array sums.
// Function to compute k maximum
// sub-array sums.
function kmax(arr, k, n)
{
// In each iteration it will give
// the ith maximum subarray sum.
for(let c = 0; c < k; c++)
{
// Kadane's algorithm.
let max_so_far = -2147483648;
let max_here = 0;
// compute starting and ending
// index of each of the sub-array.
let start = 0, end = 0, s = 0;
for(let i = 0; i < n; i++)
{
max_here += arr[i];
if (max_so_far < max_here)
{
max_so_far = max_here;
start = s;
end = i;
}
if (max_here < 0)
{
max_here = 0;
s = i + 1;
}
}
// Print out the result.
document.write("Maximum non-overlapping " +
"sub-array sum" + (c + 1) +
": " + max_so_far +
", starting index: " + start +
", ending index: " + end +
"." + "<br>");
// Replace all elements of the maximum subarray
// by -infinity. Hence these places cannot form
// maximum sum subarray again.
for(let l = start; l <= end; l++)
arr[l] = -2147483648;
}
document.write("<br>");
}
// Driver code
// Test case 1
let arr1 = [ 4, 1, 1, -1, -3,
-5, 6, 2, -6, -2 ];
let k1 = 3;
let n1 = arr1.length;
// Function calling
kmax(arr1, k1, n1);
// Test case 2
let arr2 = [ 5, 1, 2, -6,
2, -1, 3, 1 ];
let k2 = 2;
let n2 = arr2.length;
// Function calling
kmax(arr2, k2, n2);
// This code is contributed by Surbhi Tyagi.
</script>
Output:
Maximum non-overlapping sub-array sum1: 8, starting index: 6, ending index: 7.
Maximum non-overlapping sub-array sum2: 6, starting index: 0, ending index: 2.
Maximum non-overlapping sub-array sum3: -1, starting index: 3, ending index: 3.
Maximum non-overlapping sub-array sum1: 8, starting index: 0, ending index: 2.
Maximum non-overlapping sub-array sum2: 5, starting index: 4, ending index: 7.
Time Complexity: The outer loop runs for k times and kadane's algorithm in each iteration runs in linear time O(n). Hence the overall time complexity is O(k*n).
Auxiliary Space : O(1)
Similar Reads
K maximum sums of overlapping contiguous sub-arrays Given an array of Integers and an Integer value k, find out k sub-arrays(may be overlapping), which have k maximum sums. Examples: Input : arr = {4, -8, 9, -4, 1, -8, -1, 6}, k = 4 Output : 9 6 6 5Input : arr = {-2, -3, 4, -1, -2, 1, 5, -3}, k= 3 Output : 7 6 5 Using Kadane's Algorithm we can find t
13 min read
Maximize count of non-overlapping subarrays with sum K Given an array arr[] and an integer K, the task is to print the maximum number of non-overlapping subarrays with a sum equal to K. Examples: Input: arr[] = {-2, 6, 6, 3, 5, 4, 1, 2, 8}, K = 10Output: 3Explanation: All possible non-overlapping subarrays with sum K(= 10) are {-2, 6, 6}, {5, 4, 1}, {2,
6 min read
Maximum sum of non-overlapping subarrays of length atmost K Given an integer array 'arr' of length N and an integer 'k', select some non-overlapping subarrays such that each sub-array if of length at most 'k', no two sub-arrays are adjacent and sum of all the elements of the selected sub-arrays are maximum.Examples: Input : arr[] = {-1, 2, -3, 4, 5}, k = 2 O
10 min read
Count maximum non-overlapping subarrays with given sum Given an array arr[] consisting of N integers and an integer target, the task is to find the maximum number of non-empty non-overlapping subarrays such that the sum of array elements in each subarray is equal to the target. Examples: Input: arr[] = {2, -1, 4, 3, 6, 4, 5, 1}, target = 6Output: 3Expla
7 min read
Maximise array sum after taking non-overlapping sub-arrays of length K Given an integer array arr[] of length N and an integer K, the task is to select some non-overlapping sub-arrays such that each sub-array is exactly of length K, no two sub-arrays are adjacent and sum of all the elements of the selected sub-arrays is maximum.Examples: Input: arr[] = {1, 2, 3, 4, 5},
8 min read
Maximum sum two non-overlapping subarrays of given size Given an array, we need to find two subarrays with a specific length K such that sum of these subarrays is maximum among all possible choices of subarrays. Examples: Input : arr[] = [2, 5, 1, 2, 7, 3, 0] K = 2 Output : 2 5 7 3 We can choose two arrays of maximum sum as [2, 5] and [7, 3], the sum of
12 min read