Count Elements with Equal NeighborsMedium

Count Elements with Equal Neighbors

MediumPractice library · +20 XPhash mapcountingarrays

Given an array of N integers, count how many elements are equal to at least one of their immediate neighbors. An element at index i (where 0 < i < N-1) has two neighbors: A[i-1] and A[i+1]. Elements at index 0 and N-1 have only one neighbor.

Input

The first line contains an integer N. The second line contains N space-separated integers representing the array elements.

Output

Print a single integer, the count of elements satisfying the condition.

Constraints

1 <= N <= 200000 -10^9 <= A[i] <= 10^9

Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests

Example 1

Input
5
1 2 2 3 3
Output
4

Elements at index 1 (value 2) and index 2 (value 2) are equal to their neighbor(s). Elements at index 3 (value 3) and index 4 (value 3) are equal to their neighbor(s). Total count is 4.

Example 2

Input
3
5 5 5
Output
3

Element at index 0 (value 5) equals A[1]. Element at index 1 (value 5) equals A[0] and A[2]. Element at index 2 (value 5) equals A[1]. Total count is 3.

Hints