给定一个由N 个整数组成的数组arr[] ,任务是找到仅由唯一元素组成的最大子数组。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 1, 2, 3}
Output: 5
Explanation: One possible subarray is {1, 2, 3, 4, 5}.
Input: arr[]={1, 2, 4, 4, 5, 6, 7, 8, 3, 4, 5, 3, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4}
Output: 8
Explanation: Only possible subarray is {3, 4, 5, 6, 7, 8, 1, 2}.
朴素方法:解决问题的最简单方法是从给定数组生成所有子数组,并检查它是否包含任何重复项或不使用 HashSet。找到满足条件的最长子数组。
时间复杂度: O(N 3 logN)
辅助空间: O(N)
高效方法:上述方法可以通过HashMap进行优化。请按照以下步骤解决问题:
- 初始化一个变量j ,存储索引的最大值,使得索引i 和 j之间没有重复元素
- 遍历数组并根据存储在 HashMap 中的先前出现的 a[i] 不断更新j 。
- 更新j 后,相应地更新ans以存储所需子数组的最大长度。
- 遍历完成后打印ans 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find largest
// subarray with no dublicates
int largest_subarray(int a[], int n)
{
// Stores index of array elements
unordered_map index;
int ans = 0;
for (int i = 0, j = 0; i < n; i++) {
// Update j based on previous
// occurrence of a[i]
j = max(index[a[i]], j);
// Update ans to store maximum
// length of subarray
ans = max(ans, i - j + 1);
// Store the index of current
// occurrence of a[i]
index[a[i]] = i + 1;
}
// Return final ans
return ans;
}
// Driver Code
int32_t main()
{
int arr[] = { 1, 2, 3, 4, 5, 1, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << largest_subarray(arr, n);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find largest
// subarray with no dublicates
static int largest_subarray(int a[], int n)
{
// Stores index of array elements
HashMap index = new HashMap();
int ans = 0;
for(int i = 0, j = 0; i < n; i++)
{
// Update j based on previous
// occurrence of a[i]
j = Math.max(index.containsKey(a[i]) ?
index.get(a[i]) : 0, j);
// Update ans to store maximum
// length of subarray
ans = Math.max(ans, i - j + 1);
// Store the index of current
// occurrence of a[i]
index.put(a[i], i + 1);
}
// Return final ans
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 1, 2, 3 };
int n = arr.length;
System.out.print(largest_subarray(arr, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to implement
# the above approach
from collections import defaultdict
# Function to find largest
# subarray with no dublicates
def largest_subarray(a, n):
# Stores index of array elements
index = defaultdict(lambda : 0)
ans = 0
j = 0
for i in range(n):
# Update j based on previous
# occurrence of a[i]
j = max(index[a[i]], j)
# Update ans to store maximum
# length of subarray
ans = max(ans, i - j + 1)
# Store the index of current
# occurrence of a[i]
index[a[i]] = i + 1
i += 1
# Return final ans
return ans
# Driver Code
arr = [ 1, 2, 3, 4, 5, 1, 2, 3 ]
n = len(arr)
# Function call
print(largest_subarray(arr, n))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find largest
// subarray with no dublicates
static int largest_subarray(int []a, int n)
{
// Stores index of array elements
Dictionary index = new Dictionary();
int ans = 0;
for(int i = 0, j = 0; i < n; i++)
{
// Update j based on previous
// occurrence of a[i]
j = Math.Max(index.ContainsKey(a[i]) ?
index[a[i]] : 0, j);
// Update ans to store maximum
// length of subarray
ans = Math.Max(ans, i - j + 1);
// Store the index of current
// occurrence of a[i]
if(index.ContainsKey(a[i]))
index[a[i]] = i + 1;
else
index.Add(a[i], i + 1);
}
// Return readonly ans
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5, 1, 2, 3 };
int n = arr.Length;
Console.Write(largest_subarray(arr, n));
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
5
时间复杂度: O(NlogN)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live