Picking Numbers ⬀
Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to 1.
a = [1, 1, 2, 2, 4, 4, 5, 5, 5]
There are two subarrays meeting the criterion: [1, 1, 2, 2] and [4, 4, 5, 5, 5]. The maximum length subarray has 5 elements.
Complete the pickingNumbers function in the editor below.
pickingNumbers has the following parameter(s):
int a[n]: an array of integers
int: the length of the longest subarray that meets the criterion
The first line contains a single integer n, the size of the array a.
The second line contains n space-separated integers, each an a[i].
2 ≤ n ≤ 1000 < a[i] < 100- The answer will be
≥ 2.
6
4 6 5 3 3 1
3
We choose the following multiset of integers from the array: {4, 3, 3}. Each pair in the multiset has an absolute difference ≤ 1 (i.e., |4 - 3| = 1 and |3 - 3| = 0), so we print the number of chosen integers, 3, as our answer.
6
1 2 2 3 1 2
5
We choose the following multiset of integers from the array: {1, 2, 2, 1, 2}. Each pair in the multiset has an absolute difference (i.e., , , and ), so we print the number of chosen integers, , as our answer.