Erasing an element in a vector is O(n) . Doing that inside a for-loop is O(n²).
Constraints: “Sum of N over all test cases does not exceed 2⋅10^5”. In other words, if your runtime is O(n²) you will most likely end up in the range of 10^10 operations. try to stay below 10^6 or 10^7, else you will be timeouted for certain.
What you are supposed to do:
instead of removing elements from your vector, you count how many you would have removed.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define t int x;cin>>x;while(x–)
#define sort(x) sort(x.begin(),x.end())
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
t
{
long long int n;cin>>n;
vectorv(n);
vectorv1;
for(long long int i=0;i<n;i++)
{
cin>>v[i];
}
v1.push_back(v[0]);
for(long long int i=1;i<n;i++)
{
if(v[i-1]!=v[i])
{
v1.push_back(v[i]);
}
}
cout<<v1.size()<<’\n’;
}
return 0;
}
try thiss logic…!