All problems

Problem 664

Two Furthest Houses With Different Colors

Easy · 10 pts · Array, Greedy

Problem

N houses stand in a row and house i is painted with colour A[i]. Find the largest distance |i - j| between two houses with different colours. At least two colours are used. The best pair always includes the first or the last house: from the first house, look for the furthest house on the right with a different colour, and from the last house, the furthest one on the left; the answer is the larger of the two distances.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1]. At least two different colours appear.

Output

Print the maximum distance.

Constraints

2 <= N <= 100; 0 <= A[i] <= 100

Example input

7
1 1 1 6 1 1 1

Example output

3

Explanation

In 1 1 1 6 1 1 1, the house with colour 6 is at index 3, and the furthest house of a different colour is at index 0 or 6. The distance is 3, the largest possible.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Maximum Score From Removing Stones Next: Can Place Flowers