MariaSzubski
9/1/2016 - 3:02 AM

Given N sticks, reduce the length of each by the length of the smallest stick. Print the number of sticks that are left before each cut oper

Given N sticks, reduce the length of each by the length of the smallest stick. Print the number of sticks that are left before each cut operation.

/*
Solution for HackerRank > Algorithms > Implementation > Cut The Sticks
https://www.hackerrank.com/challenges/cut-the-sticks
*/

function cutSticks() {
    var n = 8;  // Number of sticks
    arr = [1,2,3,4,3,3,2,1];  // Stick length

    while (arr.length > 0){
        // Print the current number of sticks
        console.log(arr.length);

        // Sort the stick array lowest to highest
        arr.sort( (a, b) => (a - b) );

        // Subtract the lowest value from each stick
        var cut = arr[0];
        for (var i = 0; i < arr.length; i++){
            arr[i] -= cut;
        }

        // Remove the '0' sticks from the array
        var remove = arr.lastIndexOf(0) + 1;
        arr.splice(0, remove);
    }

}