Getting TLE. Help

PROBLEM CODE: Contest Page | CodeChef
In the above mentioned problem, I used array to count frequency of elements instead of maps and I am getting an TLE error. So is there no way that this problem could be solved without using map?
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
ll t;
cin>>t;
while(t–) {
ll n;
cin>>n;
ll a[n],b[n]={};
for(ll i=0;i<n;++i) {
cin>>a[i];
}
ll m=0;
for(ll i=0;i<n;++i) {
ll count=1;
for(ll j=i+1;j<n;++j) {
if(a[i]==a[j]) {
count+=1;
b[j] = -1;
}
}
if(b[i]!=-1) {
b[i] = count;
}

    }
    for(ll i=0;i<n;++i) {
        if(b[i]!=-1) {
            m+=min(a[i]-1,b[i]);
        }
    }
    cout<<m<<endl;
}
return 0;

}

Because you used nested for loop, the time complexity reached o(n*n) and the constraints on N were ≤N≤2⋅10^5., you encountered TLE (Time Limit Exceed).
You may also solve this problem with a vector pair, but the map is the simplest method to do it.

2 Likes