Subarray with largest sum after excluding its maximum element
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 arrayvoid 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 Codeint 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 subarrayimport java.util.*;class GFG{ // Function to find the maximum sum// subarray by excluding the maximum// element from the arraystatic 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 codepublic 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 arraydef 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 Codeif __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 subarrayusing System;using System.Collections.Generic;class GFG{ // Function to find the maximum sum// subarray by excluding the maximum// element from the arraystatic 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 codepublic 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 |
Output:
1 5


