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


Problem solution in Python programming.

#!/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("%d\n", 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);
    */    
}