Ilya lives in a beautiful city of Chordalsk.
There are nn houses on the street Ilya lives, they are numerated from 11 to nn from left to right; the distance between every two neighboring houses is equal to 11 unit. The neighboring houses are 11 and 22, 22 and 33, ..., n−1n−1 and nn. The houses nn and 11are not neighboring.
The houses are colored in colors c1,c2,…,cnc1,c2,…,cn so that the ii-th house is colored in the color cici. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses ii and jj so that 1≤i<j≤n1≤i<j≤n, and they have different colors: ci≠cjci≠cj. He will then walk from the house ii to the house jj the distance of (j−i)(j−i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer nn (3≤n≤3000003≤n≤300000) — the number of cities on the street.
The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤n1≤ci≤n) — the colors of the houses.
It is guaranteed that there is at least one pair of indices ii and jj so that 1≤i<j≤n1≤i<j≤n and ci≠cjci≠cj.
Output
Print a single integer — the maximum possible distance Ilya can walk.
Examples
Input
5 1 2 3 2 3
Output
4
Input
3 1 2 1
Output
1
Input
7 1 1 3 1 1 1 1
Output
4
Note
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of 5−1=45−1=4 units.
In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of 11 unit.
In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of 7−3=47−3=4 units.
题意:给你n个数,求不相同的两个数的最大下标距离
思路: 最大距离的两点一定是以左或右作为起始点
原因是如果是中间的某两点,a______b,那么a的左边一定是b,b的右边一定是a,ba_______ba,很明显原来假设的某两
点(蓝色标记的)并不是最短距离
代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int a[]=new int[300005];
for(int i=0;i<n;i++){
a[i]=scan.nextInt();
}
int max=-1;
for(int i=1;i<n;i++){
if(a[i]!=a[0])
max=Math.max(max,i);
}
for(int i=n-2;i>=0;i--){
if(a[n-1]!=a[i])
max=Math.max(max,n-1-i);
}
System.out.println(max);
}
}