AmazonOA #174
Find Minimum Possible Variance
Estimated MediumJudge-ready
Topics (AI-labelled)Hashing
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.
Input
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
Output a single integer, the answer.
Example 1
Input:
3
1 2 1
Output:
1
Example 2
Input:
2
5 5
Output:
0
Example 3
Input:
5
1 2 3 1 2
Output:
2
Example notes
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.
Editorial
Loading solution…