"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Picking Numbers - HakerRank Solution - Javascript

Picking Numbers - HakerRank Solution - Javascript

Published on 2024-11-03
Browse:685

Picking Numbers - HakerRank Solution - Javascript

Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to

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].

Solution

function pickingNumbers(a) {
    // Create an array to store frequency of each element in the input array
    let frequency = new Array(100).fill(0);

    // Count frequency of each element
    for (let i = 0; i 




          

            
        
Release Statement This article is reproduced at: https://dev.to/hokagedemehin/picking-numbers-hakerrank-solution-javascript-4mp2?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3