Source Code
/*
     Author: Asmaa Raafat
*/
/////////////////////////////"THINK TWICE... CODE ONCE"/////////////////////////////////.
#include <bits/stdc++.h>
#define ll long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
/*ll freqfac[22] ={1};
void getfac(int n)
{
    for(int i=1; i<=20; i++)
    {
        freqfac[i] = freqfac[i-1] * i;
    }
}*/
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("primes.in", "r", stdin);
    #endif
    fast;
    int t;
    cin>>t;
    while(t--)
    {
        ll n, nn=0;
        cin>>n;
        ll arr[n];
        ll cnt =0, cnt2=0;
        for(int i=0; i<n; i++)
        {
            cin>>arr[i];
        }
        if( n == 1)
        {
            cout<<1<<endl;
        }
        else
        {
            ll c = arr[n-1];
            for(int i=0; i<n-1; i++)
            {
                if(arr[i] != arr[i+1] && arr[i] != c)
                {
                    cnt++;
                    nn = i;
                }
                else if(arr[i] == arr[i+1] && arr[i] != c)
                {
                    cnt++;
                    nn = i;
                }
            }
            if(nn < n-1)
            {
                cout<<cnt+1<<endl;
            }
            else
            {
                cout<<cnt<<endl;
            }
        }
    }
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
Copy
Proud Competitors asmaa_raafat
GNU G++17
35 ms
312 KB
Wrong Answer