Can someone please help me why I got wrong answer for this code?

Revision en1, by Retr0, 2025-02-17 16:03:30

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; } } } cout<<ansl+1<<" "<<ansr+1<<endl; } }

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English Retr0 2025-02-17 21:54:19 127
en2 English Retr0 2025-02-17 16:04:00 4 Tiny change: 'n\n~~~~~\n\n\n#include' -> 'n\n~~~~~\n#include'
en1 English Retr0 2025-02-17 16:03:30 1763 Initial revision (published)