#K72722. Maximum Contiguous Subarray with Bounded Differences

    ID: 33817 Type: Default 1000ms 256MiB

Maximum Contiguous Subarray with Bounded Differences

Maximum Contiguous Subarray with Bounded Differences

Given an array of integers, find the maximum size of a subset such that the absolute difference between any two elements is at most 1. Note that the elements in the subset do not need to be consecutive in the original array.

You can determine the answer by computing the frequency of each number and then finding the maximum sum of the frequencies of two consecutive integers. Mathematically, the answer is given by:

$$\max_{x}\{freq(x)+freq(x+1)\}$$

If the array is empty, the answer is 0.

inputFormat

The first line contains an integer N, denoting the number of elements in the array. The second line contains N space-separated integers.

outputFormat

Output a single integer representing the maximum size of a subset such that the absolute difference between any two elements in the subset is at most 1.## sample

8
1 3 2 2 5 2 3 7
5

</p>