📌  相关文章
📜  具有唯一元素的最大总和连续子数组

📅  最后修改于: 2021-05-14 00:11:29             🧑  作者: Mango

给定N个正整数的数组arr [] ,任务是在所有具有唯一元素的子数组中找到具有最大和的子数组并打印其和。

天真的方法:解决问题的最简单方法是生成所有可能的子数组,并针对每个子数组检查其所有元素是否唯一。在这些子数组中找到最大和并打印出来。
时间复杂度: O(N 3 )
辅助空间: O(N)

高效方法:为了优化上述方法,其思想是使用“两指针”技术。请按照以下步骤解决该方法:

  1. 将两个指针ij分别初始化为01,以存储结果子数组的开始和结束索引。
  2. 初始化一个HashSet来存储数组元素。
  3. i = 0j = 0的空子数组开始,遍历该数组直到找到任何重复的元素,如果发现的总和大于当前的max_sum,则将其总和更新为最大和(例如max_sum )。
  4. 如果找到重复元素,则递增j并更新变量,直到在当前子数组中仅将唯一的元素从索引j保留到i为止。
  5. 对数组的其余部分重复上述步骤,并继续更新max_sum
  6. 完成上述步骤后,打印获得的最大金额。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include
using namespace std;
 
// Function to calculate required
// maximum subarray sum
int maxSumSubarray(int arr[], int n)
{
  // Initialize two pointers
  int i = 0, j = 1;
  // Stores the unique elements
  set set;
 
  // Insert the first element
  set.insert(arr[0]);
 
  // Current max sum
  int sum = arr[0];
 
  // Global maximum sum
  int maxsum = sum;
 
  while (i < n - 1 && j < n)
  {
    // Update sum & increment j
    // auto pos = s.find(3);
    const bool is_in = set.find(arr[j]) !=
                       set.end();
    if (!is_in)
    {
      sum = sum + arr[j];
      maxsum = max(sum, maxsum);
 
      // Add the current element
      set.insert(arr[j++]);
    }
 
    // Update sum and increment i
    // and remove arr[i] from set
    else
    {
      sum -= arr[i];
 
      // Remove the element
      // from start position
      set.erase(arr[i++]);
    }
  }
 
  // Return the maximum sum
  return maxsum;
}
 
// Driver Code
int  main()
{
  // Given array arr[]
  int arr[] = {1, 2, 3, 1, 5};
 
  // Function Call
  int ans = maxSumSubarray(arr, 5);
 
  // Print the maximum sum
  cout << (ans);
}
 
// This code is contributed by gauravrajput1


Java
// Java program for the above approach
 
import java.io.*;
import java.lang.Math;
import java.util.*;
public class GFG {
 
    // Function to calculate required
    // maximum subarray sum
    public static int
    maxSumSubarray(int[] arr)
    {
 
        // Initialize two pointers
        int i = 0, j = 1;
 
        // Stores the unique elements
        HashSet set
            = new HashSet();
 
        // Insert the first element
        set.add(arr[0]);
 
        // Current max sum
        int sum = arr[0];
 
        // Global maximum sum
        int maxsum = sum;
 
        while (i < arr.length - 1
               && j < arr.length) {
 
            // Update sum & increment j
            if (!set.contains(arr[j])) {
 
                sum = sum + arr[j];
                maxsum = Math.max(sum,
                                  maxsum);
 
                // Add the current element
                set.add(arr[j++]);
            }
 
            // Update sum and increment i
            // and remove arr[i] from set
            else {
 
                sum -= arr[i];
 
                // Remove the element
                // from start position
                set.remove(arr[i++]);
            }
        }
 
        // Return the maximum sum
        return maxsum;
    }
 
    // Driver Code
    public static void
        main(String[] args)
    {
        // Given array arr[]
        int arr[] = new int[] { 1, 2, 3, 1, 5 };
 
        // Function Call
        int ans = maxSumSubarray(arr);
 
        // Print the maximum sum
        System.out.println(ans);
    }
}


Python3
# Python3 program for the above approach
 
# Function to calculate required
# maximum subarray sum
def maxSumSubarray(arr):
 
    # Initialize two pointers
    i = 0
    j = 1
 
    # Stores the unique elements
    set = {}
 
    # Insert the first element
    set[arr[0]] = 1
 
    # Current max sum
    sum = arr[0]
 
    # Global maximum sum
    maxsum = sum
 
    while (i < len(arr) - 1 and
           j < len(arr)):
 
        # Update sum & increment j
        if arr[j] not in set:
            sum = sum + arr[j]
            maxsum = max(sum, maxsum)
 
            # Add the current element
            set[arr[j]] = 1
            j += 1
 
        # Update sum and increment i
        # and remove arr[i] from set
        else:
            sum -= arr[i]
 
            # Remove the element
            # from start position
            del set[arr[i]]
            i += 1
 
    # Return the maximum sum
    return maxsum
 
# Driver Code
if __name__ == '__main__':
 
    # Given array arr[]
    arr = [ 1, 2, 3, 1, 5 ]
 
    # Function call
    ans = maxSumSubarray(arr)
 
    # Print the maximum sum
    print(ans)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
 
  // Function to calculate
  // required maximum subarray sum
  public static int maxSumSubarray(int[] arr)
  {
    // Initialize two pointers
    int i = 0, j = 1;
 
    // Stores the unique elements
    HashSet set =
            new HashSet();
 
    // Insert the first element
    set.Add(arr[0]);
 
    // Current max sum
    int sum = arr[0];
 
    // Global maximum sum
    int maxsum = sum;
 
    while (i < arr.Length - 1 &&
           j < arr.Length)
    {
      // Update sum & increment j
      if (!set.Contains(arr[j]))
      {
        sum = sum + arr[j];
        maxsum = Math.Max(sum,
                          maxsum);
 
        // Add the current element
        set.Add(arr[j++]);
      }
 
      // Update sum and increment i
      // and remove arr[i] from set
      else
      {
        sum -= arr[i];
 
        // Remove the element
        // from start position
        set.Remove(arr[i++]);
      }
    }
 
    // Return the maximum sum
    return maxsum;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    // Given array []arr
    int []arr = new int[] {1, 2,
                           3, 1, 5};
 
    // Function Call
    int ans = maxSumSubarray(arr);
 
    // Print the maximum sum
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by shikhasingrajput


输出:
11




时间复杂度: O(N)
辅助空间: O(N)