给定一个由N 个正整数组成的数组arr[] ,任务是在所有具有唯一元素的子数组中找到总和最大的子数组并打印其总和。
Input arr[] = {1, 2, 3, 3, 4, 5, 2, 1}
Output: 15
Explanation:
The subarray having maximum sum with distinct element is {3, 4, 5, 2, 1}.
Therefore, sum is = 3 + 4 + 5 + 2 + 1 = 15
Input: arr[] = {1, 2, 3, 1, 5}
Output: 11
Explanation:
The subarray having maximum sum with distinct element is {2, 3, 1, 5}.
Therefore, sum is = 2 + 3 + 1 + 5 = 11.
朴素方法:解决问题的最简单方法是生成所有可能的子数组,并为每个子数组检查其所有元素是否唯一。找出这些子数组中的最大和并打印出来。
时间复杂度: O(N 3 )
辅助空间: O(N)
高效的方法:为了优化上述方法,想法是使用双指针技术。按照以下步骤解决该方法:
- 将两个指针i和j分别初始化为0和1,以存储结果子数组的开始和结束索引。
- 初始化一个HashSet来存储数组元素。
- 从i = 0和j = 0的空子数组开始,遍历数组直到找到任何重复元素,如果发现它大于当前max_sum ,则将当前总和更新为最大总和(例如max_sum )。
- 如果找到重复元素,则递增j并更新变量,直到当前子数组中从索引j到i只剩下唯一元素。
- 对数组的其余部分重复上述步骤并不断更新max_sum 。
- 打印完成上述步骤后获得的最大和。
下面是上述方法的实现:
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
Javascript
输出:
11
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live