My issue
I understand that there are a few cases
- 011: Not possible
- 0101 or 1101
- 1001
- 10001: Not possible
I can convince myself looking at these cases that
- We can’t get consecutive elements at the end
- We can’t get 3 consecutive same elements
But not 100% sure how to prove it mathematically. Please can someone help me?
My code
def is_fair_distribution(n, s):
number = 0
for i in range(n - 1, -1, -1):
if s[i] == '0':
number += 1
else:
number -= 1
if number < -1 or number > 1:
return "NO"
return "YES"
t = int(input())
for w in range(t):
n = int(input())
s = input().strip()
print(is_fair_distribution(n, s))
Problem Link: Fair Distribution Practice Coding Problem - CodeChef