#K43557. Picking Numbers

    ID: 27335 Type: Default 1000ms 256MiB

Picking Numbers

Picking Numbers

Given an array of integers, find the length of the longest subarray where the absolute difference between any two elements is at most 1. In other words, for any two elements \(a_i\) and \(a_j\) in the subarray, the condition \(|a_i - a_j| \leq 1\) must hold. This problem requires you to process the array and determine the maximum number of integers that can be chosen to satisfy the condition.

The challenge is to efficiently count frequencies and compare adjacent numbers to see which pair forms the largest valid subarray.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains a single integer \(n\) representing the number of elements in the array.
  • The second line contains \(n\) space-separated integers. Each integer is guaranteed to be in the range [1, 100].

outputFormat

Output a single integer to standard output (stdout) representing the length of the longest subarray where the absolute difference between any two numbers is at most 1, i.e. \(|a_i - a_j| \leq 1\).

## sample
6
4 6 5 3 3 1
3