iPhone 9S
InterviewTime limit1sMemory limit128 MB
Remove everyone in the line who wants one chosen capacity, then report the longest run of equal capacities that remains. Choose the capacity to maximize that run.
- Level
Medium4 of 10
- Topics
- Array, Implementation, Brute force, Simulation
- Solved
- No attempts yet
Problem
Today Apple releases the iPhone 9S. () people stand in a single line in front of the Apple Store, each wanting to be the first to buy the new iPhone.
The iPhone 9S lets each buyer choose the storage capacity freely. Older iPhones came in fixed sizes such as 16/32/64GB, but for the 9S a buyer tells the clerk the desired capacity , and an iPhone with exactly that capacity is built on the spot.
The clerk thinks the line looks nicer when people who want the same capacity stand next to each other. So the clerk picks one capacity and removes every person who wants that capacity from the line. When people leave, the remaining people keep their relative order and close the gaps.
Choose exactly one capacity, remove everyone who wants it, and look at the resulting line. Among all groups of consecutive people who want the same capacity, consider the length of the longest such group. Choose the capacity to remove so that this longest length is as large as possible, and print that maximum length.
Input
The first line contains . Each of the next lines contains one integer , the capacity wanted by each person in line order. . There are always at least two distinct values among the .
Output
Print the maximum possible length of a run of consecutive people who want the same capacity, after the clerk removes everyone who wants some single chosen capacity.
Hint
Suppose 9 people are in line and the capacities they want are, in order, 2, 7, 3, 7, 7, 3, 7, 5, 7. If everyone who wants capacity 3 is removed, the line becomes 2, 7, 7, 7, 7, 5, 7, and the longest run of the same capacity is the four consecutive people who want 7. So the answer is 4.