Cut the sticks

Cut the sticks

You are given a number of sticks of varying lengths. You will iteratively cut the sticks into smaller sticks, discarding the shortest pieces until there are none left. At each iteration you will determine the length of the shortest stick remaining, cut that length from each of the longer sticks and then discard all the pieces of that shortest length. When all the remaining sticks are the same length, they cannot be shortened so discard them.

Given the lengths of n sticks, print the number of sticks that are left before each iteration until there are none left.

For example, there are n = 3 sticks of lengths arr = [1,2,3]. The shortest stick length is 1, so we cut that length from the longer two and discard the pieces of length 1. Now our lengths are arr = [1,2]. Again, the shortest stick is of length 1, so we cut that amount from the longer stick and discard those pieces. There is only one stick left, arr = [1], so we discard that stick. Our lengths are answer = [3,2,1].

Function Description

Complete the cutTheSticks function in the editor below. It should return an array of integers representing the number of sticks before each cut operation is performed.

cutTheSticks has the following parameter(s):

  • arr: an array of integers representing the length of each stick

Input Format

The first line contains a single integer n, the size of arr.

The next line contains n space-separated integers, each an arr[i] where each value represents the length of the ith stick.

Output Format

For each operation, print the number of sticks that are present before the operation on separate lines.

Constraints

  • 1 <= n <= 1000
  • 1 <= arr[i] <= 1000

Sample Input 0

1
2
6
5 4 4 2 2 8

Sample Output 0

1
2
3
4
6
4
2
1

Explanation 0

1
2
3
4
5
6
sticks-length        length-of-cut   sticks-cut
5 4 4 2 2 8 2 6
3 2 2 _ _ 6 2 4
1 _ _ _ _ 4 1 2
_ _ _ _ _ 3 3 1
_ _ _ _ _ _ DONE DONE

Sample Input 1

1
2
8
1 2 3 4 3 3 2 1

Sample Output 1

1
2
3
4
8
6
4
1

Explanation 1

1
2
3
4
5
6
sticks-length         length-of-cut   sticks-cut
1 2 3 4 3 3 2 1 1 8
_ 1 2 3 2 2 1 _ 1 6
_ _ 1 2 1 1 _ _ 1 4
_ _ _ 1 _ _ _ _ 1 1
_ _ _ _ _ _ _ _ DONE DONE

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Complete the cutTheSticks function below.
function cutTheSticks(arr) {
let result = [];

while (arr.length > 0) {
result.push(arr.length);

arr.sort((a, b) => (a - b));

let front = arr[0];

arr.reduce((target, value, index) => {
arr[index] -= front;

return target;
}, []);

let remove = arr.lastIndexOf(0) + 1;

arr.splice(0, remove);
}

return result;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×