Picking Numbers | HackerRank Solution

Hello coders, today we are going to solve Picking Numbers HackerRank Solution which is a Part of HackerRank Algorithm Series.

Picking Numbers

Task

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.

Solution – Picking Numbers

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

Python

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

Disclaimer: The above Problem (Picking Numbers) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

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