HackerRank Picking Numbers Solution

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

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Picking Numbers Solution
HackerRank Picking Numbers Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank 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.

Function Description

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

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].


  • 2 <= n <= 100
  • 0 <= a[i] < 100
  • The answer will be =>2.

Sample Input 0

4 6 5 3 3 1

Sample Output 0


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

1 2 2 3 1 2

Sample Output 1


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) {
	// your code goes here
	int n;scanf("%d",&n);
	int arr[101];
	int i,c=0;
	{int x;
	    int t=(arr[i]+arr[i+1]);
	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);
    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++){
        int max = 0;
        for (int i = 0; i < 99; i++) {
            max = Math.max(max, a[i]+a[i+1]);

Picking Numbers Solution in Python

import sys

n = int(raw_input().strip())
a = map(int,raw_input().strip().split(' '))

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


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");

function readLine() {
    return input_stdin_array[input_currentline++];

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    a = readLine().split(' ');
    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){
        if(curRes > res){
            res = curRes;

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



Picking Numbers Solution in Pascal

Disclaimer: This problem (Picking Numbers) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.


1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment