OneCompiler

cutting ropes

238

cutting ropes

In javascript given array n having no. of ropes you have to make a cutting operation on every rope until all ropes becomes zero and cut the rope with minimum rope and return the size of the array without non-zero ropes

Following is sample javascript code.



function cutRopes(n) {

  let result = [];

  while (n.length > 0) {
    ; // add the current length of the array to the result
    let min = Math.min(...n);
    n = n.map((rope) => rope - min).filter((rope) => rope > 0);
    if(n.length!=0){
      result.push(n.length)
    }
  }

  return result;
}

let n = [5, 1, 1, 2, 3, 5];
let result = cutRopes(n);
console.log(...result)