AmazonOA #174
Find Minimum Possible Variance
估计中等可在线判题
考点(AI 标注)哈希计数
A contiguous subarray of length at least 2 whose first and last elements are equal is valid. Its variance is the subarray length minus the number of its elements equal to the endpoint value. At least one valid subarray is guaranteed to exist; find the minimum variance.
输入格式
The first line contains n; the second line contains the array. 2≤n≤200000, elements 1..10^9, and at least one value repeats.
输出格式
Output a single integer, the answer.
样例 1
输入:
3
1 2 1
输出:
1
样例 2
输入:
2
5 5
输出:
0
样例 3
输入:
5
1 2 3 1 2
输出:
2
样例说明
Example 1: the range 1, 2, 1 has only one element that is not 1, so the answer is 1. Example 2: the two 5s have nothing between them, so the answer is 0. Example 3: there are 2 other elements between the two 1s and also between the two 2s, so the answer is 2.