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 Kadane algorithm to solve this problem.



  1. As in this problem we have to choose one element which is the maximum in the subarray.
  2. 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.
  3. Finally, apply the Kadane algorithm to find the maximum sum subarray.
  4. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

1 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : chitranayal