Online C++ Compiler

#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; }