1714B - Remove Prefix - CodeForces Solution


data structures implementation

Please click on ads to support us..

Python Code:

t=int(input())
while t>0:
    n=int(input())
    array=list(map(int, input().split(' ')[:n]))
    s=set()
    ans=0
    for i in range(n-1,-1,-1):
        if array[i] in s:
            ans=i+1
            break
        else:
            s.add(array[i])
    print(ans)
    t-=1   

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,n;	cin>>t;
	while(t--){
		
		cin>>n;vector<int> a(n);
		for(int i=0;i<n;i++) cin>>a[i];
		set<int> q;
		q.clear();
		bool ok = false;
		for(int i=n-1;i>=0;i--){
			if(q.count(a[i])){
				cout<<i + 1<<endl;
				ok = true;
				break;
			}
			q.insert(a[i]);
		}
		if(!ok) cout<<0<<endl;
	}
} 


Comments

Submit
0 Comments
More Questions

Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number