1931C - Make Equal Again - CodeForces Solution


brute force greedy math

Please click on ads to support us..

Python Code:

t = int(input()) 
for _ in range(t):
    n = int(input())
    conts = [int(i) for i in input().split()]
    elem = conts[0]
    i = 1
    while i < len(conts) and conts[i] == elem :
        i += 1

    end_elem = conts[len(conts) - 1]

    j = len(conts) - 2
    while conts[j] == end_elem and j >= 0:
        j -= 1
    chosen = 0
    if end_elem == elem:
        print(len(conts[i : j + 1]))
    else:
        if i >= len(conts) - 1 - j:
            print(len(conts[i:]))
        else:
            print(len(conts[:j + 1]))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
long long T,n,a[200005];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=1;i<=n;i++)	cin>>a[i];
		long long l=1,r=n;
		while(a[l]==a[1]&&l<=n)	l++;
		while(a[r]==a[n]&&r>=l)	r--;
		if(a[1]!=a[n])	cout<<min(n-l+1,r)<<"\n";
		else	cout<<r-l+1<<"\n";
	}
}


Comments

Submit
0 Comments
More Questions

540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards