# HackerRank Picking Numbers Solution

#### ByBrokenprogrammers

Dec 12, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Picking Numbers Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## HackerRank Picking Numbers Solution

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.

Sample Input 0

```6
4 6 5 3 3 1
```

Sample Output 0

```3
```

Explanation 0

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.

Sample Input 1

```6
1 2 2 3 1 2
```

Sample Output 1

```5
```

Explanation 1

We choose the following multiset of integers from the array: {1, 2, 2, 1, 2}. Each pair in the multiset has an absolute difference <= 1 (i.e., |1 – 2| = 1, |1 1| = 0, and |2 – 2| = 0), so we print the number of chosen integers, 5, as our answer.

## HackerRank Picking Numbers Solution

### Picking Numbers Solution in C

```#include <stdio.h>
int main(void) {
int n;scanf("%d",&n);
int arr[101];
int i,c=0;
for(i=1;i<=100;i++)arr[i]=0;

for(i=0;i<n;i++)
{int x;
scanf("%d",&x);
arr[x]+=1;
}
for(i=1;i<100;i++)
{
int t=(arr[i]+arr[i+1]);
if(t>c)
c=t;
}
printf("%d",c);
return 0;
}
```

### Picking Numbers Solution in Cpp

```#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;
}```

### Picking Numbers Solution in 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[100];
for(int a_i=0; a_i < n; a_i++){
a[in.nextInt()]++;
}
int max = 0;
for (int i = 0; i < 99; i++) {
max = Math.max(max, a[i]+a[i+1]);
}
System.out.println(max);
}
}```

### Picking Numbers Solution in Python

```import sys
n = int(raw_input().strip())
a = map(int,raw_input().strip().split(' '))
a.sort()
ans = 0
for i in xrange(n):
for j in xrange(n):
if abs(a[j] - a[i]) <= 1:
ans = max(ans, j - i + 1)
print ans```

### Picking Numbers Solution using JavaScript

```process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
a = a.map(Number);
var res = 0;
for(var i=0;i<100;i++){
var numberA = i;
var numberB = i+1;
var curRes = 0;
for(var j=0;j<a.length;j++){
if(a[j] === numberA || a[j] === numberB){
curRes++;
}
}
if(curRes > res){
res = curRes;
}
}
console.log(res);
}```

### Picking Numbers Solution in Scala

```object Solution {
def main(args: Array[String]): Unit = {
val sc = new java.util.Scanner(System.in)
val n = sc.nextInt()
val nums = new Array[Int](n)
nums.indices.foreach(i => nums(i) = sc.nextInt())
val sorted = nums.sorted
val distinct = sorted.distinct
def thisAsCenter(k: Int) = {
val numEqual = sorted.count(_ == k)
val oneLess = sorted.count(_ == k-1)
val oneMore = sorted.count(_ == k+1)

numEqual + Math.max(oneLess, oneMore)
}
val solution = distinct.map(thisAsCenter(_)).max
println(solution)
}
}```

### Picking Numbers Solution in Pascal

Disclaimer: This problem (Picking Numbers) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.