I took a stab at solving this problem from Hackerrank in Javascript, and would like your valuable feedback as to time/space complexity and just about any additional improvements.
Problem URL: https://www.hackerrank.com/challenges/cut-the-sticks
My JS implementation:
function main() {
var n = parseInt(readLine());
arr = readLine().split(' ');
arr = arr.map(Number);
arr = arr.sort(function(a,b){ return a-b; });
function cut(barr){
if(barr.length>0){//checks array length
var min = barr[0]; //save min value in current array
process.stdout.write(barr.length+'\n'); //write initial array length
var b = [];
for(var i=0; i<barr.length; i++){ //iterate over array
var result = barr[i]-min; //math ops
if(result>0){
b.push(result); //build new array if value is greater than 0
}
}
cut(b); // call cut operation again with new array
}else{
return; //halt operation if no values left to check for
}
}
cut(arr);
}