Subarray with largest sum after excluding its maximum element
Last Updated :
01 Feb, 2023
Given an array arr[], the task is to find the starting and ending indices of the subarray with the largest sum after excluding its maximum element.
Examples:
Input: arr[] = {5, -2, 10, -1, 4}
Output: 1 5
Explanation:
Subarray[1:5] = {5, -2, 10, -1, 4}
Sum of subarray excluding maximum element = 5 + (-2) + (-1) + 4 = 6
Input: arr[] = {5, 2, 5, 3, -30, -30, 6, 9}
Output: 1 4
Explanation:
Subarray[1:4] = {5, 2, 5, 3}
Sum of subarray excluding maximum element = 5 + 2 + 3 = 10
Approach: The idea is to use the Kadane algorithm to solve this problem.
- As in this problem we have to choose one element which is the maximum in the subarray.
- Therefore, we can choose all the positive elements from the array, and each time we can make elements greater than that element to INT_MIN, such that it is not included in the array.
- Finally, apply the Kadane algorithm to find the maximum sum subarray.
- If there are no positive elements in the array then we can choose any one element from the array to get the maximum sum as 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximumSumSubarray( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
if (arr[i] >= 0
&& mp.find(arr[i])
== mp.end())
mp[arr[i]] = 1;
}
int first = 0;
int last = 0;
int ans = 0;
int INF = 1e6;
for ( auto i : mp) {
int mx = i.first;
int curr = 0;
int curr_start;
for ( int j = 0; j < n; j++) {
if (curr == 0)
curr_start = j;
int val = arr[j] > mx
? -INF
: arr[j];
curr += val;
if (curr < 0)
curr = 0;
if (curr > ans) {
ans = curr;
first = curr_start;
last = j;
}
}
}
cout << first + 1
<< " " << last + 1;
}
int main()
{
int arr[] = { 5, -2, 10, -1, 4 };
int size = sizeof (arr) / sizeof (arr[0]);
maximumSumSubarray(arr, size);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maximumSumSubarray( int arr[], int n)
{
Map<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (arr[i] >= 0 )
mp.put(arr[i], 1 );
}
int first = 0 ;
int last = 0 ;
int ans = 0 ;
int INF = ( int )1e6;
for (Map.Entry<Integer,
Integer> i : mp.entrySet())
{
int mx = i.getKey();
int curr = 0 ;
int curr_start = - 1 ;
for ( int j = 0 ; j < n; j++)
{
if (curr == 0 )
curr_start = j;
int val = arr[j] > mx ? -INF : arr[j];
curr += val;
if (curr < 0 )
curr = 0 ;
if (curr > ans)
{
ans = curr;
first = curr_start;
last = j;
}
}
}
System.out.print((first + 1 ) + " " +
(last + 1 ));
}
public static void main(String[] args)
{
int arr[] = { 5 , - 2 , 10 , - 1 , 4 };
int size = arr.length;
maximumSumSubarray(arr, size);
}
}
|
Python3
def maximumSumSubarray(arr, n):
mp = {}
for i in range (n):
if (arr[i] > = 0 and
arr[i] not in mp):
mp[arr[i]] = 1
first = 0
last = 0
ans = 0
INF = 1e6
for i in mp:
mx = i
curr = 0
for j in range (n):
if (curr = = 0 ):
curr_start = j
if arr[j] > mx:
val = - INF
else :
val = arr[j];
curr + = val
if (curr < 0 ):
curr = 0
if (curr > ans):
ans = curr
first = curr_start
last = j
print (first + 1 , last + 1 )
if __name__ = = "__main__" :
arr = [ 5 , - 2 , 10 , - 1 , 4 ]
size = len (arr)
maximumSumSubarray(arr, size)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void maximumSumSubarray( int []arr,
int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (arr[i] >= 0)
mp.Add(arr[i], 1);
}
int first = 0;
int last = 0;
int ans = 0;
int INF = ( int )1e6;
foreach (KeyValuePair< int ,
int > i in mp)
{
int mx = i.Key;
int curr = 0;
int curr_start = -1;
for ( int j = 0; j < n; j++)
{
if (curr == 0)
curr_start = j;
int val = arr[j] > mx ?
-INF : arr[j];
curr += val;
if (curr < 0)
curr = 0;
if (curr > ans)
{
ans = curr;
first = curr_start;
last = j;
}
}
}
Console.Write((first + 1) + " " +
(last + 1));
}
public static void Main(String[] args)
{
int []arr = {5, -2, 10, -1, 4};
int size = arr.Length;
maximumSumSubarray(arr, size);
}
}
|
Javascript
<script>
function maximumSumSubarray(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++) {
if (arr[i] >= 0
&& !mp.has(arr[i]))
mp.set(arr[i] , 1);
}
var first = 0;
var last = 0;
var ans = 0;
var INF = 1000000;
mp.forEach((value, key) => {
var mx = key;
var curr = 0;
var curr_start;
for ( var j = 0; j < n; j++) {
if (curr == 0)
curr_start = j;
var val = arr[j] > mx
? -INF
: arr[j];
curr += val;
if (curr < 0)
curr = 0;
if (curr > ans) {
ans = curr;
first = curr_start;
last = j;
}
}
});
document.write( first + 1
+ " " + (last + 1));
}
var arr = [5, -2, 10, -1, 4];
var size = arr.length;
maximumSumSubarray(arr, size);
</script>
|
Time complexity: O(N)
The time complexity of this approach is O(N) where N is the size of the given array. We iterate over the array once to store all the positive elements in a map and then iterate over the map and apply Kadane’s algorithm to find the maximum sum subarray.
Space complexity: O(N)
The space complexity of this approach is O(N) as we are storing all the positive elements in an unordered_map.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...