Remove duplicates from a sorted array with a limit on the number of repetitions

C++ Medium arrays

Task «Remove duplicates from a sorted array with a limit on the number of repetitions»

Given a non-decreasing sorted integer array. It is necessary to remove duplicates so that each element appears at most twice, and return the length of the new array. The remaining elements of the array after this length are not important. The solution must be performed without using additional memory (in-place).

Input Format

The first line contains an integer N (1 ≤ N ≤ 10^5) — the number of elements in the array. The second line contains N integers separated by spaces — the elements of the array, sorted in non-decreasing order.

Output Format

One integer — the length of the array after removing duplicates (the number of elements that appear no more than twice).

Examples

Example 1

INPUT
1 5
OUTPUT
1

Example 2

INPUT
5 1 2 3 4 5 7
OUTPUT
5

Example 3

INPUT
6 1 1 1 2 2 3
OUTPUT
5
main.cpp