Cut the sticks HackerRank Solution in C, C++, Java, Python

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.

Example

arr = [1,2,3]

The shortest stick length is 1, so cut that length from the longer two and discard the pieces of length 1. Now the lengths are arr=[1,2]. Again, the shortest stick is of length 1, so cut that amount from the longer stick and discard those pieces. There is only one stick left,arr=[1] , so discard that stick. The number of sticks at each iteration 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):

  • int arr[n]: the lengths of each stick

Returns

  • int[]: the number of sticks after each iteration

Input Format

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

The next line contains  space-separated integers, each an , where each value represents the length of the  stick.

Constraints

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

Sample Input 0

STDIN           Function

-----           --------

6               arr[] size n = 6

5 4 4 2 2 8     arr = [5, 4, 4, 2, 2, 8]

 

Sample Output 0

6

4

2

1


Explanation 0

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

8

1 2 3 4 3 3 2 1


Sample Output 1

8

6

4

1


Explanation 1

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

 

Cut the sticks HackerRank Solution in C

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int a[1002]={0};
    int i;
    for(i=0;i<n;i++)
    {
        int c;
        scanf("%d",&c);
        a[c]++;
    }
    int t=n;
    for(i=0;i<1001;i++)
    {
        if(a[i]>0)
        {
            printf("%d\n",t);
            t=t-a[i];
        }
    }
    return  0;
}

 

Cut the sticks HackerRank Solution in C++

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <map>
#include <set>

using namespace std;

#define SIZE(A) (int((A).size()))
#define pb(A) push_back(A)
#define mp(A,B) make_pair(A,B)

int a[2000000];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        scanf("%d", a+i);
    }
    sort(a, a+n);
    reverse(a, a+n);

    for (; n>0;) {
        int x = a[n-1];
        for (int i = n-1; i >= 0; i--) {
            a[i]-=x;
        }
        printf("%d\n", n);
        while (n>0 && !a[n-1]) n--;
    }

    return 0;
}

 

Cut the sticks HackerRank Solution in Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static StringBuilder out = new StringBuilder();

    public static void main(String[] args) throws IOException {
        int[] stickAmts = new int[1001];
        int numSticks = Integer.parseInt(in.readLine());
        String line = in.readLine();
        String[] data = line.split("\\s+");
        for(int i = 0; i < numSticks; i ++)
        {
            stickAmts[Integer.parseInt(data[i])]++;
        }
        for(int i = 1; i <= 1000; i ++)
        {
            if(stickAmts[i] != 0)
            {
                System.out.println(numSticks);
                numSticks -= stickAmts[i];
            }
        }
    }
}

 

Cut the sticks HackerRank Solution in Python

def solve():
    nSticks = int(raw_input())
    stickLengths = [int(x) for x in raw_input().split()]
    cuts = 0
    totalCutLength = 0
    stickLengths = sorted(stickLengths)
    i = 0
    while i < len(stickLengths):
        print len(stickLengths)-i
        totalCutLength = stickLengths[i]
        while i < len(stickLengths) and stickLengths[i] <= totalCutLength:
            i += 1

solve()

 

Cut the sticks HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CutTheStick
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split(' ');

            int[] sticks = new int[n];
            for (int i = 0; i < n; i++)
                sticks[i] = int.Parse(s[i]);

            Array.Sort(sticks);
            int stick = n;
            int current = sticks[0];
            int count = 1;

            for (int i = 1; i < n; i++)
            {
                if (sticks[i] == current)
                    count++;
                else
                {
                    Console.WriteLine(stick);
                    stick -= count;
                    count = 1;
                    current = sticks[i];
                }
            }
            Console.WriteLine(stick);
        }
    }
}

 

Attempt Cut the Sticks HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/cut-the-sticks/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/non-divisible-subset-hackerrank-solution/

Leave a Comment