Retr0's blog

By Retr0, history, 4 days ago, In English

Solved

I am getting wrong answer at test case 427. The problem link is here and my solution link is here

#include <bits/stdc++.h>
using namespace std;
 
int main(){
 
    int T;cin>>T;
    while(T--){
        int n;cin>>n;
        vector<int> a(n);
 
        map<int,int> mp;
 
        for(int i=0;i<n;i++){
            cin>>a[i];
            mp[a[i]]++;
        } 
 
        bool ok = true;
        for(auto x:a){
            if(mp[x] == 1){
                ok = false;
                break;
            }
        }
        if(ok){
            cout<<0<<endl;
            continue;
        }
 
        int l = -1, r = -1;
        int cnt = 0;
        int len = -INF;
        int ansl = -1, ansr = -1;
        int f = 0;
 
 
        for(int i=0;i<n;i++){
            if(mp[a[i]] == 1 && f == 0){
                l = i;
                f = 1;
            }
            if(mp[a[i]] == 1 && f == 1){
 
                cnt++;
                if(i == n-1){
                    r = i;
                    f = 0;
                    if(len < cnt){
                        len = cnt;
                        ansl = l;
                        ansr = r;
                    }
                } 
            }
            if(mp[a[i]] > 1 && f == 1){
                r = i-1;
                f = 0;
                if(len < cnt){
                    len = cnt;
                    ansl = l;
                    ansr = r;
                }
                // here I forgot to write cnt = 0 after finding probable solution
                // cnt = 0;
            }
        }
        cout<<ansl+1<<" "<<ansr+1<<endl;
 
    }
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By Retr0, history, 3 years ago, In English

Ah! Solved the first problem on codeforces today. :)

Full text and comments »

  • Vote: I like it
  • +17
  • Vote: I do not like it