Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,056 views

 

 

in Online Assessments by Expert (111,530 points) | 1,056 views

1 Answer

0 like 0 dislike

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int b[1000001];
unordered_map<int,int> mapi;

int main() {
    int n ;
    cin>>n;
    int i=1;
    while(i<=n){
        cin>>b[i];
        if (mapi[b[i]]==0){
            mapi[b[i]]=i;
        }
        i++;
    }
    
  
    int cnt=0;
    i=n;
    while(i>=1){
        if( (mapi[b[i]]==i)  ||  mapi[b[i]]>i){
            cnt++;
            i--;
        }
        else i=mapi[b[i]]-1;
    }
    cout<<cnt;
    return 0;
    }

by (140 points)
reshown by