Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Picking Numbers problem solution

YASH PAL, 31 July 202430 November 2025

In this HackerRank Picking Numbers problem You have Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to 1.

HackerRank Picking Numbers problem solution

Hackerrank Picking Numbers solution in Python.

#!/bin/python3

import sys

def count(n, a):
    return len(list(filter( (lambda x: x==n), a)))

n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]

mi = min(a)
ma = max(a)
maxCount = -1
if mi == ma:
    print(len(a))
else:
    for i in range(mi, ma):
            c = count(i, a) + count(i+1, a)
            if c > maxCount:
                maxCount = c
            
    print(maxCount)



Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution
{
	public static void main(String[] args)
	{
		Scanner readIn = new Scanner(System.in);
		
		int n = readIn.nextInt();
		int[] frequencie = new int[100];
		for(int ii = 0; ii < n; ii++)
			frequencie[readIn.nextInt()]++;
		
		int out = Integer.MIN_VALUE;
		for(int ii = 0; ii < frequencie.length - 1; ii++)
			out = frequencie[ii] + frequencie[ii + 1] > out ? frequencie[ii] + frequencie[ii + 1] : out;
		
		System.out.println(out);
	}
}

Problem solution in C++ programming.

#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("%dn", ans);
    return 0;
}

Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int answer(int *, int);
int main(){
    int n,i,j,temp; 
    scanf("%d",&n);
    int *a = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%d",&a[a_i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(a[j] < a[i])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
    printf("%d",answer(a,n));
    return 0;
}
int answer(int a[],int n)
    {
    int i,j,count=0,max=0;
    for(i=0;i<n;i++)
        {
        count = 0;
        for(j=i+1;j<n;j++)
            {
            if((a[j]-a[i]==1)||(a[j]-a[i]==0))
                count++;
        }
        if(max<count)
            {
            max = count;
        }
        else
            {
            
        }
    }
    return max+1;
}

Problem solution in JavaScript programming.

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();    
});

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 sorted = a.sort(function(a, b) {
        return a - b;
    });
    
    var i = 0;
    var j = 1;
    
    while(i < sorted.length && j < sorted.length) {
        if(Math.abs(sorted[i] - sorted[j]) > 1) {
            i++;
            j++;
        } else {
            j++;
        }
    }
    console.log(Math.abs(i - j));
    /*  
        console.log(selectedNumbers);
        console.log(selectedNumbers.length);
    */    
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes