We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
IMHO while the target of this task is to show how pre-sorting is helpful for some other tasks, there is simple JS solution:
1) extract nums into additional array
2) sort asc this array (it doesn't matter how to do this, so I use built-in 'sort' method)
3) calc values in one loop 0..99 using results for previous array items
let result = new Array();
let num_arr = arr.map(value => value.split(' ')[0]);
num_arr.sort((a,b)=>a-b);
let j = 0;
for (let i=0; i<100; i++){
while (num_arr[j]<=i) j++;
result[i] = j;
}
return result;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counting Sort 3
You are viewing a single comment's thread. Return to all comments →
IMHO while the target of this task is to show how pre-sorting is helpful for some other tasks, there is simple JS solution: 1) extract nums into additional array 2) sort asc this array (it doesn't matter how to do this, so I use built-in 'sort' method) 3) calc values in one loop 0..99 using results for previous array items