Picking Numbers

Picking Numbers

Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1. For example, if your array is a = [1,1,2,2,4,4,5,5,5], you can create 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. It should return an integer that represents the length of the longest array that can be created.

pickingNumbers has the following parameter(s):

  • a: an array of integers

Input Format

The first line contains a single integer n, the size of the array a.

The second line contains n space-separated integers a[i].

Constraints

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

Output Format

A single integer denoting the maximum number of integers you can choose from the array such that the absolute difference between any two of the chosen integers is <= 1.

Sample Input 0

1
2
6
4 6 5 3 3 1

Sample Output 0

1
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

1
2
6
1 2 2 3 1 2

Sample Output 1

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* Complete the 'pickingNumbers' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY a as parameter.
*/
function pickingNumbers(a) {
// Write your code here
let max = 0;
let values = new Array(100).fill(0);

(a || []).forEach(value => {
values[value]++;
});

return values.reduce((target, value, index) => {
(
(index >= 1) &&
(value + values[index - 1] > target)
) && (target = value + values[index - 1]);

return target;
}, []);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×