A roster lists n seats in a row, each holding a team id. Two team ids x and y are singled out. Find the length of the longest contiguous window in which team x and team y appear the same number of times. Seats holding other team ids do not affect the balance and may be included freely. A window with zero of both x and y counts as balanced. If no window is balanced, the answer is 0.
Line 1: three integers n, x, and y (with x != y).
Line 2: n space-separated integers, the team ids in seat order.
A single integer: the length of the longest window with equal counts of x and y.
Example 1
Input
5 1 2 1 1 2 3 3
Expected
4
Explanation
The window seats 1..4 (values 1 2 3 3) has one 1 and one 2, and the trailing 3s do not change the balance, giving length 4.
Example 2
Input
6 1 2 1 2 1 2 3 3
Expected
6
Explanation
Across the whole roster team 1 and team 2 each appear twice, so the entire length-6 window is balanced.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →