Maximum number of Unique integers in Sub- Array of given sizes in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array of size n and a number M. Our task is to create a program that will find the maximum number of unique integers in Sub-array of given size.

Here, we will have to find the sub-array of size M that has the maximum number of unique elements.

Let’s take an example to understand the problem,

Input − array = {4, 1, 2, 1 , 4, 3}. M = 4

Output − 4

Explanation

All possible combinations of sub-arrays of size 4.
{4, 1, 2, 1} = 3 unique elements
{1, 2, 1, 4} = 3 unique elements
{2, 1, 4, 3} = 4 unique elements

To solve this problem, we will simply generate all subarrays of size M and then count the number of unique elements in the subarray. And check if the number of unique elements is greater than the global maximum unique element count, and store the greatest of both. But this solution is not efficient.

A better solution will be using the sliding window technique. In which we create a window of size m and then use a hash table for storing unique elements for the current window.

We will create the window in the following way −

  • Create a window of size M and store the elements in a hash map.

  • Then move to the window for the element at (M+1) position and removing the element for the previous window.

Let’s see this solution from our example to understand the solution better −

Array = {4, 1, 2, 1, 4, 3}

Window size, M = 4.

WindowHash MapNumber of Unique elementsElement addedElement deleted
{4,1,2,1}4,1,23--
{1,2,1,4}1,2,4344
{2,1,4,3}2,1,4,3431

This solution shows how the window and hash Map are formed and the number of unique elements is counted.

Example

Program to find the maximum number of unique integers in Sub-array of a given size −

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int maxUniqueElement(int a[],int N,int M){
   map<int,int> hashMap;
   int uniqueCountWindow=0;
   int uniqueCount=0;
   for(int i=0;i<M;i++) {
      if(hashMap.find(a[i])==hashMap.end()) {
         hashMap.insert(make_pair(a[i],1));
         uniqueCountWindow++;
      }
      else
         hashMap[a[i]]++;
      }
      uniqueCount = uniqueCountWindow;
      for(int i=M;i<N;i++) {
         if(hashMap[a[i-M]]==1) {
            hashMap.erase(a[i-M]);
            uniqueCountWindow--;
         }
         else
            hashMap[a[i-M]]--;
            if(hashMap.find(a[i])==hashMap.end()){
               hashMap.insert(make_pair(a[i],1));
               uniqueCountWindow++;
            }
            else
               hashMap[a[i]]++;
               uniqueCount=max(uniqueCount,uniqueCountWindow);
         }
      return uniqueCount;
}
int main(){
   int arr[] = {4, 1 ,2, 1, 4, 3};
   int M=4;
   int N=sizeof(arr)/sizeof(arr[0]);
   cout<<"The maximum number of unique elements in sub-array of size "<<M<<" is "<<maxUniqueElement(arr,N,M)<<endl;
}

Output

The maximum number of unique elements in sub-array of size 4 is 4
raja
Published on 10-Jan-2020 08:00:56
Advertisements