📜  排除最大元素后的总和最大的子数组

📅  最后修改于: 2021-05-19 19:35:11             🧑  作者: Mango

给定一个数组arr [] ,任务是在排除其最大元素之后找到总和最大的子数组的开始和结束索引。
例子:

方法:想法是使用Kadane算法来解决此问题。

  1. 就像这个问题一样,我们必须选择一个元素,它是子数组中的最大值。
  2. 因此,我们可以从数组中选择所有正元素,并且每次使大于该元素的元素变为INT_MIN时,就不会将其包含在数组中。
  3. 最后,应用Kadane算法找到最大和子数组。
  4. 如果数组中没有正元素,那么我们可以从数组中选择任何一个元素,以将最大总和设为0。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum sum subarray such by
// excluding the maximum element
// from the subarray
 
#include 
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 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 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 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 mp = new Dictionary();
 
  // 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 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


输出:
1 5