HackerRank Picking Numbers Problem Solution Yashwant Parihar, April 14, 2023April 14, 2023 In this post, We are going to solve HackerRank Picking Numbers Problem. 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 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 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 (e. [4-3] and [3-3]. 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 (ie.. [1-2]. and [2-2] =0). so we print the number of chosen integers. 5, as our answer. HackerRank Picking Numbers Problem Solution Picking Numbers C Solution #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int a[101]; void bubble(int n) { int swap=0,i,j,k; for(i=1;i<n;i++){ if(a[i]>a[i+1]){ j=a[i+1]; a[i+1]=a[i]; a[i]=j; swap++; } } if(swap!=0) bubble(n-1); } int main(){ int n,i,j,k,l,t,p[101]; scanf("%d",&n); for(i = 1; i <= n; i++){ scanf("%d",&a[i]); p[i]=0;} l=1; t=1; bubble(n); for(i=1;i<=n;i++){ l=1; for(j=i+1;j<=n;j++){ if(a[j]-a[i]==0) { i++; l++; } else if(a[j]-a[i]==1){ l++; } } if(t<l) t=l; } printf("%d\n",t); return 0; } Picking Numbers C++ Solution #include <bits/stdc++.h> using namespace std; #define pii pair<int , int > #define inf 1111111111 #define in(a) scanf("%d", &a) #define ins(a) scanf("%s", a) #define in2(a, b) scanf("%d%d", &a, &b) #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c) #define mp make_pair #define vi vector<int > #define _ceil(n, a) ((n)%(a)==0?((n)/(a)):((n)/(a)+1)) #define cl clear() #define sz size() #define pn printf("\n") #define pr(a) printf("%d\n", a) #define prs(a) printf("%d ", a) #define pr2(a, b) printf("%d %d\n", a, b) #define pr3(a, b, c) printf("%d %d %d\n", a, b, c) #define pb push_back #define mem(a, b) memset((a), (b), sizeof(a)) #define all(X) (X).begin(), (X).end () #define iter(it, X) for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++) #define ext(a) {printf("%s\n", a); return 0;} #define oka(x, y) ((x)>=0&&(x)<row&&(y)>=0&&(y)<col) #define x first #define y second #define lc (2*i) #define rc (2*i+1) #define sst clock_t t = clock() #define eed printf ("It took me %d clicks.\n", (int)(clock() - t)) #define prr(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); pn;} vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template<typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cerr <<a<<" "; err(++it, args...); } typedef long long LL; //int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction //int dx[]={1,1,0,-1,-1,-1,0,1,0};int dy[]={0,1,1,1,0,-1,-1,-1,0};//8 direction //int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction //bool check(int n, int pos) {return (n & (1<<pos))>>pos;} //typecast 1 in case of int //int on(int n, int pos) {return n | (1<<pos);} //typecast 1 in case of int //int off(int n, int pos) {return n & ~(1<<pos);} //typecast 1 in case of int //bool operator < (const data &d) const{return cost<d.cost;} //reverse in priority queue const int M = 300, mod = 1000000007; int A[M]; int main() { int n, i, j, k; in(n); for (i=0; i<n; i++) in(A[i]); int maxx = 0; sort(A, A+n); for (i=0; i<n; i++) { for (j=i+1; j<n; j++) { if (A[j] - A[i] <= 1) maxx = max(maxx, j-i+1); } } pr(maxx); return 0; } Picking Numbers C Sharp Solution using System; using System.Collections.Generic; using System.Linq; class Solution { static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); int[] counts = new int[100]; foreach (int x in a) { counts[x - 1]++; } int maxCount = 0; for (int i = 0; i < 99; i++) { maxCount = Math.Max(maxCount, counts[i] + counts[i + 1]); } Console.WriteLine(maxCount); } } Picking Numbers java Solution 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(); } Arrays.sort(a); int max = 0, co; int i = 0; while(i<n-1) { int j = i+1; co = 1; while(j<n && a[j]-a[i] <= 1) { j++; co++; } if(co > max) max = co; i++; } System.out.println(max); } } Picking Numbers JavaScript Solution 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); */ } Picking Numbers Python Solution #!/bin/python3 import sys n = int(input().strip()) a = [int(a_temp) for a_temp in input().strip().split(' ')] max = 0 a = sorted(a) #print(a) for i in range(n-1): nbr = 1 for j in range(i+1, n): if a[j]-a[i]<=1: nbr += 1 #print("i={0}, j={1}, a[{0}]={2}, a[{1}]={3}, diff = {4}, nbr={5}".format(i,j,a[i],a[j],a[j]-a[i],nbr)) if nbr > max: max = nbr else: if nbr > max: max = nbr break print(max) Other Solution HackerRank Climbing the Leaderboard Solution HackerRank The Hurdle Race Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython