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++
// C++ implementation to find the
// maximum sum subarray such by
// excluding the maximum element
// from the subarray
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum sum
// subarray by excluding the maximum
// element from the array
void maximumSumSubarray(int arr[], int n)
{
unordered_map<int, int> mp;
// Loop to store all the positive
// elements in the map
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;
// Loop to iterating over the map
// and considering as the maximum
// element of the current including
// subarray
for (auto i : mp) {
// Make the current
// element maximum
int mx = i.first;
int curr = 0;
int curr_start;
// Iterate through array and
// apply kadane's algorithm
for (int j = 0; j < n; j++) {
if (curr == 0)
curr_start = j;
// Condition if current element is
// greater than mx then make
// the element -infinity
int val = arr[j] > mx
? -INF
: arr[j];
curr += val;
if (curr < 0)
curr = 0;
if (curr > ans) {
ans = curr;
// Store the indices
// in some variable
first = curr_start;
last = j;
}
}
}
cout << first + 1
<< " " << last + 1;
}
// Driver Code
int main()
{
int arr[] = { 5, -2, 10, -1, 4 };
int size = sizeof(arr) / sizeof(arr[0]);
// Function Call
maximumSumSubarray(arr, size);
return 0;
}
Java
// Java implementation to find the
// maximum sum subarray such by
// excluding the maximum element
// from the subarray
import java.util.*;
class GFG{
// Function to find the maximum sum
// subarray by excluding the maximum
// element from the array
static void maximumSumSubarray(int arr[], int n)
{
Map<Integer, Integer> mp = new HashMap<>();
// Loop to store all the positive
// elements in the map
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;
// Loop to iterating over the map
// and considering as the maximum
// element of the current including
// subarray
for (Map.Entry<Integer,
Integer> i : mp.entrySet())
{
// Make the current
// element maximum
int mx = i.getKey();
int curr = 0;
int curr_start = -1;
// Iterate through array and
// apply kadane's algorithm
for(int j = 0; j < n; j++)
{
if (curr == 0)
curr_start = j;
// Condition if current element is
// greater than mx then make
// the element -infinity
int val = arr[j] > mx ? -INF : arr[j];
curr += val;
if (curr < 0)
curr = 0;
if (curr > ans)
{
ans = curr;
// Store the indices
// in some variable
first = curr_start;
last = j;
}
}
}
System.out.print((first + 1) + " " +
(last + 1));
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 5, -2, 10, -1, 4 };
int size = arr.length;
// Function call
maximumSumSubarray(arr, size);
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation to find
# the maximum sum subarray such
# by excluding the maximum
# element from the subarray
# Function to find the maximum sum
# subarray by excluding the maximum
# element from the array
def maximumSumSubarray(arr, n):
mp = {}
# Loop to store all the positive
# elements in the map
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
# Loop to iterating over the map
# and considering as the maximum
# element of the current including
# subarray
for i in mp:
# Make the current
# element maximum
mx = i
curr = 0
# Iterate through array and
# apply kadane's algorithm
for j in range(n):
if (curr == 0):
curr_start = j
# Condition if current element
# is greater than mx then make
# the element -infinity
if arr[j] > mx:
val =- INF
else:
val= arr[j];
curr += val
if (curr < 0):
curr = 0
if (curr > ans):
ans = curr
# Store the indices
# in some variable
first = curr_start
last = j
print(first + 1, last + 1)
# Driver Code
if __name__ == "__main__":
arr = [ 5, -2, 10, -1, 4 ]
size = len(arr)
# Function Call
maximumSumSubarray(arr, size)
# This code is contributed by chitranayal
C#
// C# implementation to find the
// maximum sum subarray such by
// excluding the maximum element
// from the subarray
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum sum
// subarray by excluding the maximum
// element from the array
static void maximumSumSubarray(int []arr,
int n)
{
Dictionary<int,
int> mp = new Dictionary<int,
int>();
// Loop to store all the positive
// elements in the map
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;
// Loop to iterating over the map
// and considering as the maximum
// element of the current including
// subarray
foreach (KeyValuePair<int,
int> i in mp)
{
// Make the current
// element maximum
int mx = i.Key;
int curr = 0;
int curr_start = -1;
// Iterate through array and
// apply kadane's algorithm
for(int j = 0; j < n; j++)
{
if (curr == 0)
curr_start = j;
// Condition if current element is
// greater than mx then make
// the element -infinity
int val = arr[j] > mx ?
-INF : arr[j];
curr += val;
if (curr < 0)
curr = 0;
if (curr > ans)
{
ans = curr;
// Store the indices
// in some variable
first = curr_start;
last = j;
}
}
}
Console.Write((first + 1) + " " +
(last + 1));
}
// Driver code
public static void Main(String[] args)
{
int []arr = {5, -2, 10, -1, 4};
int size = arr.Length;
// Function call
maximumSumSubarray(arr, size);
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript implementation to find the
// maximum sum subarray such by
// excluding the maximum element
// from the subarray
// Function to find the maximum sum
// subarray by excluding the maximum
// element from the array
function maximumSumSubarray(arr, n)
{
var mp = new Map();
// Loop to store all the positive
// elements in the 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;
// Loop to iterating over the map
// and considering as the maximum
// element of the current including
// subarray
mp.forEach((value, key) => {
// Make the current
// element maximum
var mx = key;
var curr = 0;
var curr_start;
// Iterate through array and
// apply kadane's algorithm
for (var j = 0; j < n; j++) {
if (curr == 0)
curr_start = j;
// Condition if current element is
// greater than mx then make
// the element -infinity
var val = arr[j] > mx
? -INF
: arr[j];
curr += val;
if (curr < 0)
curr = 0;
if (curr > ans) {
ans = curr;
// Store the indices
// in some variable
first = curr_start;
last = j;
}
}
});
document.write( first + 1
+ " " + (last + 1));
}
// Driver Code
var arr = [5, -2, 10, -1, 4];
var size = arr.length;
// Function Call
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.
Similar Reads
Maximum Subarray Sum Excluding Certain Elements Given an array of A of n integers and an array B of m integers find the Maximum Contiguous Subarray Sum of array A such that any element of array B is not present in that subarray. Examples : Input : A = {1, 7, -10, 6, 2}, B = {5, 6, 7, 1} Output : 2 Explanation Since the Maximum Sum Subarray of A i
15+ min read
Maximum sum subarray after altering the array Given an array arr[] of size N. The task is to find the maximum subarray sum possible after performing the given operation at most once. In a single operation, you can choose any index i and either the subarray arr[0...i] or the subarray arr[i...N-1] can be reversed. Examples: Input: arr[] = {3, 4,
15+ min read
Maximize product of subarray sum with its maximum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of the subarray sum with the maximum element of that subarray. Examples: Input: arr[] = {2, -3, 8, -2, 5}Output: 88Explanation:The required maximum product can be obtained using subarray {8, -2, 5}Therefo
8 min read
Maximize the maximum subarray sum after removing atmost one element Given an array arr[] of N integers, the task is to find the maximum sum of a subarray with at most one deletion allowed. The size of subarray to be considered should be at least 1.Examples: Input: arr[] = {1, 2, 3, -2, 3} Output: 9 The maximum sub-array sum is given by the sub-array {2, 3, -2, 3} He
2 min read
Maximum subarray sum with same first and last element formed by removing elements Given an array arr[] of N integers, the task is to find the maximum sum of subarray having length an at least 2 whose first and last elements are the same after removing any number of array elements. If there exists no such array, then print 0. Examples: Input: arr[] = {-1, -3, -2, 4, -1, 3}Output:
6 min read