1625B - Elementary Particles - CodeForces Solution


brute force greedy sortings *1100

Please click on ads to support us..

Python Code:

T = int(input())
for _ in range(T):
    n = int(input())
    a = list(map(int, input().split()))
    marker = [-1] * 150001
    ans = 150001
    for i in range(n):
        if marker[a[i]] == -1:
            marker[a[i]] = i
        else:
            ans = min(ans, i - marker[a[i]])
            marker[a[i]] = i
    if ans == 150001:
        print(-1)
    else:
        print(n - ans)

C++ Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll tt;
     tt=1;
    cin >> tt;
    while (tt--)
    {    
      ll n;
      cin >> n;
      vector<ll> v(n);
      map<ll,ll> mp;
      ll mn = INT_MAX;
      for( ll i = 0; i < n; i++ ){
            cin >> v[i];

            if(mp.find(v[i])!=mp.end()){
                mn = min(mn, (i-mp[v[i]]));
                mp[v[i]]=i;
            }
            else{
                mp[v[i]]=i;
            }
      }

      if(mn==INT_MAX){
        cout<<-1<<"\n";
      }
      else{
        cout<<n-mn<<"\n";
      }
    } 
    return 0;
}


Comments

Submit
0 Comments
More Questions

1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64