Maximum number of Unique integers in Sub-Array of given size
Given an array of N integers and a number M. The task is to find out the maximum number of unique integers among all possible contiguous subarrays of size M.
Examples:
Input : arr[] = {5, 3, 5, 2, 3, 2}, M = 3
Output : 3
Explanation:
In the sample test case, there are 4 subarrays of size 3.
s1 = (5, 3, 5)- Has 2 unique numbers.
s2 = (3, 5, 2)- Has 3 unique numbers.
s3 = (5, 2, 3)- Has 3 unique numbers.
s4 = (2, 3, 2)- Has 2 unique numbers.
In these subarrays, there are 2, 3, 3, 2 unique numbers, respectively.
The maximum amount of unique numbers among all possible contiguous subarrays is 3.
Input : arr[] = {5, 5, 5, 5, 5, 5}, M = 3
Output : 1
Naive Approach:
- Generate all subarrays of size M.
- Count unique number for each subarray.
- Check whether it is greater than the previous maximum unique number or not, if yes, replace it with the previous maximum unique number.
- Continue until we generate all possible subarrays.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int maxUniqueNum( int a[], int N, int M)
{
int maxUnique=0;
for ( int i=0;i<=N-M;i++)
{
set< int > s;
for ( int j=0;j<M;j++)
{
s.insert(a[i+j]);
}
if (s.size()>maxUnique)
{
maxUnique=s.size();
}
}
return maxUnique;
}
int main()
{
int arr[] = {5, 3, 5, 2, 3, 2};
int M=3,N= sizeof (arr)/ sizeof (arr[0]);
cout<<maxUniqueNum(arr,N,M)<<endl;
}
|
Java
import java.util.*;
class GFG {
public static int maxUniqueNum( int arr[],
int N, int M)
{
int maxUnique = 0 ;
for ( int i = 0 ; i <= N - M; i++) {
int currentUnique = 0 ;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for ( int k = i; k < i + M; k++) {
if (!map.containsKey(arr[k])) {
map.put(arr[i], 1 );
currentUnique++;
}
}
if (currentUnique > maxUnique)
maxUnique = currentUnique;
}
return maxUnique;
}
public static void main(String[] args)
{
int [] arr = { 5 , 3 , 5 , 2 , 3 , 2 };
int N = 6 ;
int M = 3 ;
System.out.println(maxUniqueNum(arr, N, M));
}
}
|
Python3
def maxUniqueNum(a, N, M):
maxUnique = 0
for i in range (N - M + 1 ):
s = set ()
for j in range (M):
s.add(a[i + j])
if ( len (s) > maxUnique):
maxUnique = len (s)
return maxUnique
if __name__ = = '__main__' :
arr = [ 5 , 3 , 5 , 2 , 3 , 2 ]
M = 3
N = len (arr)
print (maxUniqueNum(arr, N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int maxUniqueNum( int []arr,
int N, int M)
{
int maxUnique = 0;
for ( int i = 0; i <= N - M; i++)
{
int currentUnique = 0;
Dictionary< int , int > map = new Dictionary< int , int >();
for ( int k = i; k < i + M; k++)
{
if (!map.ContainsKey(arr[k]))
{
map.Remove(arr[i]);
map.Add(arr[i], 1);
currentUnique++;
continue ;
}
}
if (currentUnique > maxUnique)
maxUnique = currentUnique;
}
return maxUnique;
}
public static void Main(String[] args)
{
int [] arr = { 5, 3, 5, 2, 3, 2 };
int N = 6;
int M = 3;
Console.WriteLine(maxUniqueNum(arr, N, M));
}
}
|
Javascript
<script>
function maxUniqueNum(arr,N,M)
{
let maxUnique = 0;
for (let i = 0; i <= N - M; i++) {
let currentUnique = 0;
let map = new Map();
for (let k = i; k < i + M; k++) {
if (!map.has(arr[k])) {
map.set(arr[i], 1);
currentUnique++;
}
}
if (currentUnique > maxUnique)
maxUnique = currentUnique;
}
return maxUnique;
}
let arr=[5, 3, 5, 2, 3, 2 ];
let N = 6;
let M = 3;
document.write(maxUniqueNum(arr, N, M));
</script>
|
Time Complexity : O(M * N)
Auxiliary Space : O(M)
Efficient Solution An efficient solution is to use window sliding technique. We maintain a single hash table for storing unique elements of every window.
1) Store counts of first M elements in a hash map.
2) Traverse from (M+1)-th element and for every element, add it to hash map and remove first element of previous window.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int max_U_element( int a[], int N, int M)
{
map< int , int > hash;
int dist_count=0;
int res=0;
for ( int i=0;i<M;i++)
{
if (hash.find(a[i])==hash.end())
{
hash.insert(make_pair(a[i],1));
dist_count++;
}
else
{
hash[a[i]]++;
}
}
res=dist_count;
for ( int i=M;i<N;i++)
{
if (hash[a[i-M]]==1)
{
hash.erase(a[i-M]);
dist_count--;
}
else
{
hash[a[i-M]]--;
}
if (hash.find(a[i])==hash.end())
{
hash.insert(make_pair(a[i],1));
dist_count++;
}
else
{
hash[a[i]]++;
}
res=max(res,dist_count);
}
return res;
}
int main()
{
int arr[] = {1, 2, 1, 3, 4, 2, 3};
int M=4,N= sizeof (arr)/ sizeof (arr[0]);
cout<<max_U_element(arr,N,M)<<endl;
}
|
Java
import java.util.HashMap;
class maxUniqueNumWindow {
static int maxUniqueNum( int arr[], int M)
{
HashMap<Integer, Integer> hM = new HashMap<Integer, Integer>();
int dist_count = 0 ;
for ( int i = 0 ; i < M; i++) {
if (hM.get(arr[i]) == null ) {
hM.put(arr[i], 1 );
dist_count++;
}
else {
int count = hM.get(arr[i]);
hM.put(arr[i], count + 1 );
}
}
int res = dist_count;
for ( int i = M; i < arr.length; i++) {
if (hM.get(arr[i - M]) == 1 ) {
hM.remove(arr[i - M]);
dist_count--;
}
else
{
int count = hM.get(arr[i - M]);
hM.put(arr[i - M], count - 1 );
}
if (hM.get(arr[i]) == null ) {
hM.put(arr[i], 1 );
dist_count++;
}
else
{
int count = hM.get(arr[i]);
hM.put(arr[i], count + 1 );
}
res = Math.max(res, dist_count);
}
return res;
}
public static void main(String arg[])
{
int arr[] = { 1 , 2 , 1 , 3 , 4 , 2 , 3 };
int M = 4 ;
System.out.println(maxUniqueNum(arr, M));
}
}
|
Python3
def max_U_element(a, N, M):
hsh = dict ()
dist_count = 0
res = 0
for i in range (M):
if (arr[i] not in hsh.keys()):
hsh[a[i]] = 1
dist_count + = 1
else :
hsh[a[i]] + = 1
res = dist_count
for i in range (M, N):
if (a[i - M] in hsh.keys() and hsh[a[i - M]] = = 1 ):
del hsh[a[i - M]]
dist_count - = 1
else :
hsh[a[i - M]] - = 1
if (a[i] not in hsh.keys()):
hsh[a[i]] = 1
dist_count + = 1
else :
hsh[a[i]] + = 1
res = max (res, dist_count)
return res
arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 3 ]
M = 4
N = len (arr)
print (max_U_element(arr, N, M))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxUniqueNum( int []arr, int M)
{
Dictionary< int ,
int > hM = new Dictionary< int ,
int >();
int dist_count = 0;
for ( int i = 0; i < M; i++)
{
if (!hM.ContainsKey(arr[i]))
{
hM.Add(arr[i], 1);
dist_count++;
}
else
{
int count = hM[arr[i]];
hM[arr[i]] = count + 1;
}
}
int res = dist_count;
for ( int i = M; i < arr.Length; i++)
{
if (hM[arr[i - M]] == 1)
{
hM.Remove(arr[i - M]);
dist_count--;
}
else
{
int count = hM[arr[i - M]];
hM[arr[i - M]] = count - 1;
}
if (!hM.ContainsKey(arr[i]))
{
hM.Add(arr[i], 1);
dist_count++;
}
else
{
int count = hM[arr[i]];
hM[arr[i]] = count + 1;
}
res = Math.Max(res, dist_count);
}
return res;
}
public static void Main(String []arg)
{
int []arr = { 1, 2, 1, 3, 4, 2, 3 };
int M = 4;
Console.WriteLine(maxUniqueNum(arr, M));
}
}
|
Javascript
<script>
function maxUniqueNum(arr,m)
{
let hM = new Map();
let dist_count = 0;
for (let i = 0; i < M; i++) {
if (hM.get(arr[i]) == null ) {
hM.set(arr[i], 1);
dist_count++;
}
else {
let count = hM.get(arr[i]);
hM.set(arr[i], count + 1);
}
}
let res = dist_count;
for (let i = M; i < arr.length; i++) {
if (hM.get(arr[i - M]) == 1) {
hM. delete (arr[i - M]);
dist_count--;
}
else
{
let count = hM.get(arr[i - M]);
hM.set(arr[i - M], count - 1);
}
if (hM.get(arr[i]) == null ) {
hM.set(arr[i], 1);
dist_count++;
}
else
{
let count = hM.get(arr[i]);
hM.set(arr[i], count + 1);
}
res = Math.max(res, dist_count);
}
return res;
}
let arr=[1, 2, 1, 3, 4, 2, 3];
let M = 4;
document.write(maxUniqueNum(arr, M));
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(M)
Last Updated :
26 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...