**Picking Numbers**

### Problem Statement :

Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to 1. Example 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. Function Description Complete the pickingNumbers function in the editor below. pickingNumbers has the following parameter(s): int a[n]: an array of integers Returns int: the length of the longest subarray that meets the criterion Input Format 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]. Constraints 2 <= n <= 100 0 < a[i] < 100 The answer will be >= 2

### Solution :

` ````
Solution in C :
python 3 :
#!/bin/python3
import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
from collections import Counter
d = Counter(a)
best = 0
for i in range(99):
best = max(d[i] + d[i+1], best)
print(best)
Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
int[] freq = new int[100];
for(int i = 0; i < a.length; ++i)
{
freq[a[i]]++;
}
int curBest = 0;
for(int i = 0; i < 99; ++i)
{
curBest = Math.max(curBest, freq[i]+freq[i+1]);
}
System.out.println(curBest);
}
}
C++ :
#include <bits/stdc++.h>
using namespace std;
int N;
int A[1000];
int main()
{
scanf("%d", &N);
for(int i=0; i<N; i++)
{
int a;
scanf("%d", &a);
A[a]++;
}
int ans=0;
for(int i=1; i<1000; i++)
ans=max(ans, A[i-1]+A[i]);
printf("%d\n", ans);
return 0;
}
```

## View More Similar Problems

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →