We need to solve: Given a binary array of length n, determine if it can be represented as several alternating black and white stripes of equal width. That is, the array can be partitioned into consecutive segments (stripes) where each segment is all the same color (0 or 1), and colors alternate between segments. Moreover, each segment must have the same length (let's denote that length as k). Note that the number of segments m can be any positive integer, and k must be positive integer such that mk = n. Also, the stripes alternate, starting with either 0 or 1. Also, note that if there is only one stripe (m=1), then trivially it's all same color and any width works (k=n). So we need to check if there exists an integer k (>=1) dividing n such that for all i in [0, m-1] (where m = n/k), the segment from ik+1 to (i+1)*k is constant and the colors alternate.
But careful: The description says "the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width." That implies that the stripes alternate, i.e., the colors switch each stripe. So if there is more than one stripe, they must alternate: first stripe any color, next opposite, etc. And all stripes have the same width.
Thus we need to test if the array matches that pattern for some k dividing n.
Naive: try all divisors of n up to 100k (max 128 maybe). For each divisor k, compute m = n/k, check that each block of length k is constant (all same value) and that the values of consecutive blocks are alternating. This check can be done in O(n) per divisor, total O(n * tau(n)) worst-case about n * sqrt(n) but n max 100k, tau(n) max around 128? Actually number of divisors for numbers up to 100k is at most 192? Let's check: 100800 has many divisors? The maximum tau for numbers up to 100k is maybe 192? Actually 83160 has 128 divisors. So it's fine. But we can be more efficient.
Alternate approach: Count runs of consecutive equal values. Let the runs be: run lengths l1, l2, ..., lm where m is number of runs (>=1). The condition for the zebra pattern with equal stripe width is that all run lengths are equal to some k, and the colors alternate. However, note that it's possible that there is only one run (m=1) then it's fine. Also, if m>1, then we require that all run lengths are equal. But is that sufficient? Suppose we have runs lengths: [k, k, k] with alternating colors? Actually alternating colors automatically if runs are contiguous and colors differ each time. So we could just compute the run lengths and check that all run lengths are equal. But careful: The array could be something like: 0 0 1 1 0 0. Here runs: [2,2,2] with colors: 0,1,0 alternating? Yes they alternate. So that would be valid. However, what about: 0 0 0 1 1 1 0 0 0 1 1 1? That's runs: [3,3,3,3] alternating colors, valid. What about: 0 0 1 1 1 0? runs: [2,3,1] not all equal -> invalid. So the simple condition "all runs have same length" seems to be necessary and sufficient? Let's check more subtle cases: Suppose we have pattern 0 0 1 1 0 0 1 1? runs: [2,2,2,2] all equal, alternating colors? Colors: 0,1,0,1 alternates properly. So valid.
But is there any case where all runs are equal but the colors do not alternate? Since runs are defined as maximal consecutive equal bits, by definition adjacent runs have opposite colors (because if they were same, they'd be merged). So colors always alternate. So indeed, if we compute runs, the pattern is exactly alternating colors. So the condition reduces to: either only one run (any length) OR multiple runs all having the same length. That's it. But wait, is it possible that the photo is a zebra but the runs are not all equal? Consider a zebra pattern of equal width k, but maybe there are runs that are not all equal? If we have alternating black/white stripes of equal width, then each stripe is a run (since each stripe is contiguous and same color). And since stripes have equal width, all runs lengths are equal. So yes, the condition is necessary.
But what about the possibility that there is only one stripe? That's a single run. So condition holds.
Thus the solution appears to be: Traverse array, compute consecutive runs lengths. If number of runs is 1: YES. Else: check if all run lengths are identical. If yes, YES else NO.
But is that always correct? Let's test examples:
Ex1: 0 0 0 1 1 1 0 0 0 -> runs: three runs: lengths 3,3,3 all equal -> YES.
Ex2: 0 0 0 1 1 1 1 -> runs: lengths 3,4 not all equal -> NO.
Ex3: 1 1 1 1 1 -> one run -> YES.
Ex4: 1 1 1 0 0 0 1 1 -> runs: lengths 3,3,2 not all equal -> NO.
Ex5: 1 1 0 1 1 0 1 1 0 -> runs? 1,1,0,1,1,0,1,1,0. Let's compute: start with 1: first two ones -> run length 2? Actually sequence: [1,1,0,1,1,0,1,1,0]. Starting at index1: 1, then 1, then 0: run of 1's length 2. Then run of 0's length 1. Then run of 1's length 2. Then run of 0's length 1. Then run of 1's length 2. Then run of 0's length 1. So runs: [2,1,2,1,2,1] not all equal -> NO. So matches output.
But wait, there could be an edge case: what if there are multiple runs but they have equal length, but the array is not exactly alternating? But runs by definition are maximal, so they always alternate. So no problem.
But wait, what if the array is something like: 0 0 1 1 0 0. That's runs: [2,2,2] all equal -> YES. That is a zebra pattern with k=2, m=3? But stripes of equal width would be three stripes: white(0) width2, black(1) width2, white(0) width2. That's alternating black and white stripes? Yes, stripes alternate colors. So valid.
But is there any case where runs are all equal but the pattern wouldn't be considered a zebra because the stripes might not start and end with same color? No, that's allowed: "the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width." It doesn't require that the first and last are different, nor that there is an even number. So odd number of stripes is fine.
So the run-length approach seems correct.
However, let's double-check the problem statement: "You think that this photo can contain a zebra. In this case the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width." This phrasing could be interpreted as: The photo is of a zebra, so there must be stripes: black and white alternating. They say "several (possibly, only one) alternating black and white stripes". The phrase "alternating black and white stripes" might imply that if there is more than one stripe, they must alternate. So our interpretation is correct.
One nuance: Could there be a scenario where the photo contains multiple runs but not all runs have equal length, yet it could still be considered a zebra because the zebra's stripes might be of equal width but the photo might have extra columns? For example, if the zebra stripes are of width k, but the photo might be taken at an angle? No, the problem is deterministic: the photo is a sequence of columns, each all white or all black. For it to be a zebra, the entire photo should consist of several alternating black and white stripes of equal width. That means the array must exactly be comprised of stripes of equal width. So runs must be all equal. So no.
Thus solution: compute run lengths, if count==1 or all lengths equal: YES else NO.
But we must ensure that when there are multiple runs, all lengths are exactly equal. However, consider a case: n=1, single element: that's one run -> YES.
What about n=2: [0,1] -> runs: [1,1] all equal -> YES (zebra with k=1, two stripes). That's correct.
What about [0,0,1,0]? runs: [2,1,1] -> not all equal -> NO. Could it be considered zebra with equal stripe width? Suppose we try k=1? Then segments: [0],[0],[1],[0] but segments: they must be constant and alternating? The segments would be: first segment length1 is 0, second 0? That would not alternate because two consecutive zeros are same color, but the pattern requires stripes alternating, so they cannot be adjacent same color stripes. So no. Could be k=2? Then segments: [0,0] (0) and [1,0] (not constant because has both 1 and 0) -> invalid. So NO.
Thus correct.
But wait, could there be a case where runs are equal but the stripes aren't of equal width because the runs might be merged? For example: Suppose we have array: [0,0,1,1,0,0,1,1]. Runs: [2,2,2,2] all equal -> YES. That's fine.
So the run-length method works.
But is there any scenario where the zebra pattern could have stripes of equal width but the runs as we compute might not correspond exactly to stripes? What if a stripe is composed of multiple runs because the stripe is not constant? But the definition says each stripe is all white or all black, so it's constant. So each stripe is a run. So indeed runs = stripes.
Thus problem reduces to: either all elements are same (one run) or the array can be partitioned into alternating runs of equal length.
But is there any hidden requirement that the stripes must be alternating in the sense that they go black-white-black-... or white-black-white-...? That is inherent.
Thus solution is O(n). That's efficient.
But before finalizing, we must also consider the input format: "If ai is zero, the i-th column is all black. If ai is one, then the i-th column is all white." So 0 means black column, 1 means white. So we treat bits as given.
Implementation: read n, read list a of length n. Then compute run lengths: iterate i from 0 to n-1, count current run length, when value changes or end, record length. Then if number of runs == 1: print YES. Else: get the first run length, check all other runs equal to that. If yes, print YES else NO.
Edge Cases: If there are multiple runs and all lengths are equal, but the first run length could be any number. That's fine.
But wait, is there any scenario where the runs are all equal but there is an extra trailing incomplete stripe? No, because total length must be multiple of run length. If all runs are equal length L, then total length = L * runsCount. That's fine.
Thus solution accepted.
But I recall some Codeforces problem "Zebra" maybe had twist? Let's search memory: There is a Codeforces problem "A. Zebra" or something? Actually I recall problem "Zebra" from Codeforces Round? Possibly 21A? Not sure. But I've seen similar simple solution.
But before final answer, I'll test some more cases:
- n=4: [0,0,1,1] -> runs: [2,2] -> YES. This is stripes: black width2, white width2.
- n=4: [0,1,0,1] -> runs: [1,1,1,1] -> YES (k=1).
- n=4: [0,1,1,0] -> runs: [1,2,1] -> not equal -> NO.
- n=6: [0,0,1,1,0,0] -> runs: [2,2,2] -> YES.
- n=6: [0,0,0,1,1,1] -> runs: [3,3] -> YES.
- n=6: [0,0,1,1,1,1] -> runs: [2,4] -> NO.
- n=2: [0,0] -> runs: [2] -> YES (only one stripe). But is that a zebra? According to problem, "possibly, only one" stripe, so yes. So all-white or all-black photo is considered zebra. That matches example 3.
Thus solution seems fine.
But what about n=3: [0,1,0] -> runs: [1,1,1] -> YES (k=1). That's valid zebra: black, white, black stripes each width1. That's plausible.
Now consider tricky case: n=0? n>=1, so ignore.
Thus solution is simple.
But wait, the problem says "each column of this photo is all white or all black." So we have columns. The stripes are vertical stripes across the whole height? That is fine.
Thus final answer: compute runs lengths, then check condition.
Let's also think about alternative interpretation: Could the stripes be of equal width but there might be more than 2 stripes? Yes we handled.
Potential pitfalls: The input numbers are given as integers, might be separated by spaces. In Python, reading is easy.
Time complexity: O(n). Memory: O(1) aside from storing array maybe we can process on the fly. But we need to compute runs, can do it streaming.
Thus solution code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
a = list(map(int, data[1:1+n]))
runs = []
i = 0
while i < n:
j = i
while j < n and a[j] == a[i]:
j += 1
runs.append(j - i)
i = j
if len(runs) == 1:
print("YES")
else:
first = runs[0]
ok = all(l == first for l in runs)
print("YES" if ok else "NO")
Edge case: If all runs equal, okay.
But wait, what about the possibility that there are multiple runs but the first run length might be different from the rest? That's the check.
But consider case: n=5: [0,0,1,1,0] -> runs: [2,2,1] not all equal -> NO. Could it be zebra with k=1? Then stripes would be: 0,0,1,1,0 -> but then the stripes would be length1 each? That would be: [0],[0],[1],[1],[0] but segments of length1 would be constant trivially, but they would need to be alternating colors. However we have two consecutive zeros, so they would be two black stripes adjacent, which violates alternating because stripes should alternate. So invalid. So our check yields NO, correct.
Now consider case: [0,0,1,1,0,0] -> runs: [2,2,2] -> YES. That's good.
Thus solution stands.
But I want to double-check if there is any scenario where the runs are all equal but the pattern is not alternating? No.
Thus answer.
But wait, there is a nuance: The problem says "the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width." The phrase "alternating black and white stripes" could be interpreted as the stripes must be alternating in the sense that they go black, white, black, white,... or white, black, white,... So they must strictly alternate. That's satisfied by runs.
Thus the solution is correct.
Now let's implement in Python.
But to be thorough, let's consider the possibility of trailing newline or spaces. We'll handle.
Now some people might think about "must be exactly k stripes of equal width"? But it's ambiguous: "several (possibly, only one) alternating black and white stripes of equal width." Means: there are some stripes; each stripe has same width; and they alternate. So it's exactly as we said.
Thus final answer.
But wait, I recall a Codeforces problem "Zebra" that had something like "print the length of each stripe" or something else. This seems different.
But I'm confident.
Let's also test the following: n=4, [0,1,0,0]? runs: [1,1,2] -> NO. Could be zebra with k=2? Segments: [0,1] not constant; [0,0] constant but alternating? No.
n=8: [0,0,0,0,1,1,1,1] -> runs: [4,4] -> YES.
n=8: [0,0,1,1,0,0,1,1] -> runs: [2,2,2,2] -> YES.
n=8: [0,0,1,1,0,0,0,0] -> runs: [2,2,4] -> NO.
Thus okay.
Thus answer: YES if all runs equal length (including single run).
Therefore solution is trivial.
But wait: Might there be a case where runs are not all equal but the pattern could still be considered zebra because the stripes could be of equal width but some stripes could be composed of multiple runs? That would contradict the definition of stripe as all same color. So no.
Thus finalize.
But before finishing, let's think about potential trick: Could there be a scenario where the stripes have equal width but there is a run that is shorter than that width because the stripes are adjacent but same color? For example, suppose the intended stripes are of width 2, but the array is [0,0,0,1,1,0,0]? That would be runs: [3,2,2] not all equal, so not valid. Could we rearrange? No.
Thus fine.
Implementation details: In Python, we can also use itertools.groupby.
But simpler: manual loop.
We'll output YES/NO accordingly.
We'll include the solution.
Edge case: n=100000, all same: runs list length 1 -> YES.
Edge case: alternating every element: runs length n each length 1 -> YES.
Thus code.
But wait, there is a tricky scenario: If there is only one run, it's considered a zebra. That's consistent with example 3 (all ones). So yes.
Thus final answer.
Now produce the final answer with explanation.
But the problem statement says: "If ai is zero, the i-th column is all black. If ai is one, then the i-th column is all white." This is opposite of typical maybe but it's fine.
One more check: Example 1: 0 0 0 1 1 1 0 0 0 -> they output YES. Our runs: three runs of length 3, so YES. Example 2: NO. Example 3: all 1's, runs: 1, length 5 -> YES. Example 4: 1 1 1 0 0 0 1 1 -> runs: [3,3,2] -> NO. Example 5: [1,1,0,1,1,0,1,1,0] -> runs: [2,1,2,1,2,1] -> NO.
Matches.
Thus final solution.
Now I'll produce the final answer in the required format: print "YES" or "NO". We'll include the code.
But for thoroughness, we might ask: Could there be a scenario where the number of runs is 0? No.
Now I'll present the answer.
Let's also consider if the input might have extra spaces, but we handle.
Thus final answer.
But wait, some might argue that for a zebra, the stripes must be alternating black and white, and the number of stripes should be even? Because a zebra typically has black and white stripes both sides? But the problem statement says "several (possibly, only one) alternating black and white stripes". It doesn't require even count. So odd is allowed.
Thus fine.
Now produce final answer.
I'll now write the solution with explanation. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1 + n]))
# compute lengths of consecutive runs
runs = []
i = 0
while i < n:
j = i
while j < n and a[j] == a[i]:
j += 1
runs.append(j - i)
i = j
if len(runs) == 1:
print("YES")
else:
first_len = runs[0]
ok = all(length == first_len for length in runs)
print("YES" if ok else "NO")
if name == "main":
solve()