We need to find the longest contiguous segment of array where no two adjacent elements are equal. That's essentially longest subarray where for all i in segment (except possibly last), a[i] != a[i+1]. Equivalent to longest subarray where the adjacent differences are all nonzero.
So it's like we need to find longest segment where there is no equal adjacent pair inside the segment. The condition only concerns adjacent houses. So we can think of scanning and whenever we see a[i]==a[i-1] that breaks continuity. So we can split array into blocks where each block is maximal sequence such that adjacent elements are different. But careful: If we have a segment where all adjacent are different, then it's a contiguous block where no two adjacent equal. So the longest such segment is simply the longest run where a[i]!=a[i-1] for all consecutive within that run. That's like longest sequence of non-equal consecutive pairs. But is it that simple? Let's test with example: 1 2 3 3 2 1 2 2. The longest block where no adjacent equal: we can start at index 1: 1,2,3 -> adjacent 2 and 3 are different, but next is 3 which equals previous 3, so break. So block length 3. Next we can start at index 3? Actually after break at 3 and 4 are equal, we can start new block at index 4? But index 4 is 3, then we have 2 (diff), 1 (diff), 2 (diff), then next 2 equal, so block from index 4 to 7: colors 3,2,1,2 length 4. That's longest. So the problem reduces to: find the longest contiguous subarray where for all i from L to R-1, a[i]!=a[i+1].
This can be solved by scanning with two pointers: maintain left index, and for each right, if a[right] == a[right-1] then we must move left to right (or right) because the new segment cannot include both of these equal adjacent? Actually the condition is that the segment must have no equal adjacent pairs. If we extend right and find that a[right]==a[right-1], then the segment from left to right contains an equal adjacent pair (at right-1 and right). So we need to start a new segment at right (i.e., left = right) because any segment that includes both these equal elements would violate condition. But we could also move left to right? Let's think: If we have segment [L,R] and we just found that a[R]==a[R-1], then the pair (R-1,R) is bad. To get a valid segment ending at R, we must exclude at least one of them. The typical sliding window approach: we want longest valid window. When we add a new element at right, if it creates a violation (adjacent equal) with previous element, then we need to move left to right (i.e., start new window at right) because any window that includes both R-1 and R is invalid; but windows that end at R could start at R (single element) or after R-1? Actually we could consider left to be R (so window contains only element R) or left to be R-1? If we move left to R-1? Then window is [R-1,R] includes the bad pair. So no. If we move left to R, then window is [R,R] valid. If we move left to any index <= R-1, the window includes the pair (R-1,R) because both are in range if left <= R-1 and right = R. So any window ending at R that is valid must have left > R-1, i.e., left = R. So indeed left must be set to R. But what about windows ending at R that start earlier but skip some indices? Our windows are contiguous, so if left <= R-1, then both R-1 and R are included. Thus the only valid windows ending at R are those with left = R. So we reset left to R.
Now what about violation not just at the boundary? But as we expand, we maintain that current window [L,R] has no equal adjacent pairs. So when we add R+1, we only need to check a[R]==a[R+1]? Actually adjacent pairs inside window are only those between consecutive indices. Since window before was valid, all internal adjacent pairs are fine. The only new adjacent pair is between R and R+1. So if a[R]!=a[R+1], then window extended remains valid. If a[R]==a[R+1], then it becomes invalid and we must reset left to R+1 (starting new window at that element). That's the algorithm.
But is there any nuance? Let's test with array: 1,1,2,2,3,3. Starting left=1, right=1 (valid). Add right=2: check a1==a2? 1==1, violation, so left=2. Then add right=3: check a2==a3? 1!=2, ok, window [2,3] length 2. Add right=4: check a3==a4? 2==2 violation, left=4. Then window [4,4] length1, then add right=5: a4!=a5? 2!=3 ok, window [4,5] length2, add right=6: a5==a6? 3==3 violation, left=6. So max length 2. That's correct. But is it always the case that the longest such segment is simply the longest block between consecutive equal pairs? Essentially we are splitting the array at positions where a[i]==a[i-1], and each maximal segment between such splits is a block where all consecutive are different? Let's check: In example, splits at indices where a[i]==a[i-1] are at i=4 (a3=3, a4=3) and i=8 (a7=2, a8=2). So segments: [1,3], [4,7], [8,8]. Lengths 3,4,1, max 4. That matches.
But is it always true that the longest valid segment is exactly one of these blocks? Consider array: 1,2,1,2,1,2. There are no adjacent equal pairs. Then the whole array is a block, length 6, valid. That's fine.
Consider array: 1,2,1,1,2,1. Splits at i=4 (a3=1, a4=1). So blocks: [1,3] length3, [4,6] length3? [4,6] colors 1,2,1 -> adjacent 1-2 diff, 2-1 diff, so valid, length 3. So max 3. Could there be a longer segment that crosses a split? Suppose we have a split at i where a[i]==a[i-1]. Any segment that includes both i-1 and i would have that equal adjacent pair, so invalid. So crossing is impossible. Thus longest valid segment must be entirely within one of the blocks defined by splitting at positions of equality. So the problem reduces to finding the maximum length of a contiguous segment that does not contain any i such that a[i]==a[i-1] (except maybe at boundaries?). Actually the block is maximal segment where for all internal indices j (from L+1 to R), a[j]!=a[j-1]. So each block is exactly a maximal such segment. So answer = max length of these blocks.
But careful: Could there be a valid segment that starts in one block and ends in another, skipping the equal pair? For contiguous segment, if it includes indices on both sides of a split, it necessarily includes the two adjacent equal elements because they are consecutive indices. Unless we exclude one of them. But if we skip, then it wouldn't be contiguous because the split is just two indices that are consecutive. To have a contiguous segment that contains indices before i-1 and after i, it must include all indices between them, including both i-1 and i. So it's invalid. So indeed, the segment cannot cross an equality pair. So answer is max block length.
Thus the solution is extremely simple: iterate through array, keep current block length. Start with length=1 for first element. For i from 2 to n, if a[i]==a[i-1] then reset current length to 1; else increment current length. Keep track of max length.
But wait: There is nuance? The condition is "no two adjacent houses at the road have the same color." That means for any two adjacent houses in the selected segment, they must have different colors. That is exactly what we used. So answer is length of longest subarray where for all consecutive pairs, elements are distinct.
So the solution is O(n). The problem constraints up to 100k, trivial.
But the problem statement includes k (number of colors) which is not used in this approach, but it's given; maybe they expect some other interpretation? Could there be a misinterpretation? Let's read problem again: "Anya likes walking along this road, but she doesn't like when two adjacent houses at the road have the same color. She wants to select a long segment of the road such that no two adjacent houses have the same color." That seems clear: She wants a segment where for every adjacent pair within that segment, colors differ.
But why include k? Maybe there is some twist: "long segment of the road such that no two adjacent houses have the same color." Could be interpreted as: She wants a segment where all houses have distinct colors from their immediate neighbors, but not necessarily that all consecutive pairs in the segment are distinct? No, "adjacent houses at the road" meaning houses that are next to each other along the road. In the selected segment, any two adjacent houses (within the segment) must not have the same color. So yes.
But maybe they meant something else: She doesn't like when two adjacent houses have the same color anywhere on the road. She wants to select a segment where this property holds (i.e., there is no occurrence of two adjacent houses with same color within the segment). So same.
Hence answer is as above.
But given that k is provided, maybe they originally intended a different problem: maybe to find longest segment where all colors are distinct? Or maybe something about number of colors? But no, the statement says "no two adjacent houses at the road have the same color." So only adjacent.
Thus answer = longest subarray without consecutive equal elements. That is standard.
But let's double-check example: Input 8 3, colors: 1 2 3 3 2 1 2 2. Our algorithm: start len=1 (first). i=2: 2!=1 -> len=2, max=2. i=3: 3!=2 -> len=3, max=3. i=4: 3==3 -> reset len=1. i=5: 2!=3? Actually after reset at i=4, current len is for block starting at 4. At i=5, we check a5(2) vs a4(3) -> 2!=3 -> len=2 (block indices 4-5). max=3. i=6: a6=1 vs a5=2 diff -> len=3, max=3. i=7: a7=2 vs a6=1 diff -> len=4, max=4. i=8: a8=2 vs a7=2 equal -> reset len=1. So max=4. Works.
Thus answer = maximum length.
Edge cases: n=1, answer=1. All houses same color: then any segment with length >1 has adjacent equal, so answer = 1. Our algorithm: start len=1, i=2: equal -> reset len=1; so max remains 1. Good.
Time complexity O(n), memory O(1).
But wait, there might be nuance: The problem might be from Codeforces or some contest. I recall a problem "Longest k-Good Segment" or something, but that's different. However, this problem could be trick: maybe she doesn't like when any two adjacent houses (anywhere on the road, not just inside selected segment) have same color, but she selects a segment such that in the whole road, there are no adjacent houses with same color? That doesn't make sense.
Given that they included k, maybe the intended problem is different: Perhaps she wants a segment where all adjacent houses have different colors, meaning that for the segment, we don't have any pair of consecutive equal, but maybe we can consider that the segment length includes houses that are not necessarily consecutive? No, it's a segment (contiguous).
Alternatively, maybe they ask for the longest segment where every two houses (not necessarily adjacent) have different colors? That would be longest subarray with all distinct elements. But they said "adjacent". So that's not it.
Another possibility: "no two adjacent houses at the road have the same color" could be a condition for the whole road. She selects a segment such that property holds for the whole road? But that would be weird: "no two adjacent houses at the road have the same color" is a condition that she doesn't like. She wants to select a long segment such that this condition is avoided, i.e., there is no occurrence of two adjacent houses with same color within that segment. So same.
Thus, the presence of k is likely irrelevant, maybe just to indicate that colors are from 1..k. But sometimes they ask to find the longest segment where all adjacent houses have different colors, which is independent of k.
Thus solution is trivial.
But wait, maybe the problem is more subtle: Because there is an example: 8 3, colors: 1 2 3 3 2 1 2 2, output 4. So that matches.
Thus I think it's correct.
But could there be a case where the longest valid segment is not exactly a block between equal adjacent pairs? Suppose we have array: 1,2,1,1,2,2,3. The blocks: between equal adjacents: positions: i=4 (a3=1, a4=1), i=6 (a5=2, a6=2). So blocks: [1,3] length3 (1,2,1), [4,5] length2 (1,2), [6,7] length2 (2,3). Actually [4,5] is 1,2? Let's compute: indices: 1:1,2:2,3:1,4:1,5:2,6:2,7:3. Block1: from 1 until before first equality at i=4, so indices 1-3 (1,2,1) length3. After reset at i=4, we start block2 at index4. At i=5, check a5=2 vs a4=1 diff, so block2 length2 (indices4-5). At i=6, a6=2 vs a5=2 equal -> reset block3 at index6. i=7: a7=3 vs a6=2 diff -> block3 length2. So max=3. Could there be a segment that crosses from block2 to block3? That would include indices 5 and 6 which are equal (2,2). So invalid. So max remains 3.
Thus algorithm stands.
Hence, answer is maximum length of consecutive elements where each adjacent pair is different.
Implementation:
int n, k;
cin >> n >> k;
vector a(n);
for(int i=0;i<n;i++) cin>>a[i];
int maxlen=1, curlen=1;
for(int i=1;i<n;i++){
if(a[i]==a[i-1]){
curlen=1;
} else {
curlen++;
if(curlen>maxlen) maxlen=curlen;
}
}
cout << maxlen << endl;
But careful: maxlen should be at least 1, since n>=1. So set maxlen=1 initially, curlen=1. After each update, update maxlen.
But we could also compute max after increment.
Alternatively, we can compute the lengths of blocks directly.
Given simplicity, we can just output the maximum.
But is there any scenario where the answer might be larger than n? No.
Thus solution is O(n) time, O(1) space.
But note that input may be large, so use fast I/O.
Now, I'll provide the solution code in C++.
But the user said: "You are ChatGPT ... respond with final answer inside code block and maybe some explanation." They want the solution. So I'll output a brief explanation and the code.
However, note: the problem might be from some contest where there is a twist: maybe they ask for the longest segment where for any two adjacent houses, the colors are different, but they gave k, maybe because they expect using k to do something else like counting colors? No, it's not needed.
Another thought: Could "adjacent houses at the road" mean houses that are adjacent in the segment, but also houses that are adjacent in the original road but possibly not consecutive indices? No, adjacent means consecutive.
Thus answer.
But I'll double-check problem statement: "There are n houses along the road where Anya lives, each one is painted in one of k possible colors. Anya likes walking along this road, but she doesn't like when two adjacent houses at the road have the same color. She wants to select a long segment of the road such that no two adjacent houses have the same color." So yes.
Thus the solution is straightforward.
I'll produce final answer.
Given that it's easy, but they might expect an explanation for the sliding window.
Now, I will provide code.
Implementation details:
- Read n and k (k ignored).
- Read array a of size n.
- Initialize ans = 1, cur = 1.
- For i from 1 to n-1:
if a[i] == a[i-1] cur = 1 else { cur++; ans = max(ans, cur); }
- Print ans.
Edge case: n=0? Not possible, n>=1.
Thus done.
I'll add includes and main.
One thing: Since k up to 100k and a_i up to k, we can store in int.
Now produce final code.
Now, maybe there is a catch: The problem might be about selecting a segment such that for any two houses that are adjacent (i.e., next to each other) in the segment, they have different colors, but the segment could be non-contiguous? No, segment implies contiguous.
Thus final answer.
Now write final. The longest valid segment corresponds to the longest contiguous block where no two adjacent houses have the same color.
We can find it by scanning the array and resetting the current length whenever a[i] equals a[i-1].
The answer is the maximum length encountered.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
int ans = 1, cur = 1;
int prev, x;
cin >> prev; // first house
for (int i = 1; i < n; ++i) {
cin >> x;
if (x == prev) {
cur = 1; // start new block
} else {
++cur;
ans = max(ans, cur);
}
prev = x;
}
cout << ans << '\n';
return 0;
}