Solution(explained with comments)


#1

vector Solution::dNums(vector &A, int B) {
//IN MAP KEY=A[] AND VALUE=INDEX OF THAT NUMBER

//IN THIS SOLUTION,IF WE GET A NO THAT IS ALREADY PRESENT WE UPDATE THE 
//INDEX IN THE MAP

unordered_map<int,int> map;
vector<int> ans;

int i=0;
int j=B-1;

//add the N number to map
for(int k=i;k<=j;k++){
map[A[k]]=k;
}

while(j<A.size()){
//we update the value if present
map[A[j]]=j;

   ans.push_back(map.size());
   
    //we only delete the previous i if the 
   //index of previous i element as same as in map

   if(map.count(A[i])>0 && map[A[i]]==i){
       map.erase(A[i]);
   }
   
   i++;
   j++;

}

return ans;

}