Cut the sticks - HackerRank || HackerRank Cut the sticks Solution

Problem Name: Cut the sticks
Judge: HackerRank
Problem link: Cut the sticks



HackerRank Cut the sticks Solution in C

#include <stdio.h>

int main()
{
    int n, temp, count;
    scanf("%d", &n);
    int array[n];
    for (int i = 0; i < n; i++) scanf("%d", &array[i]);
    printf("%d\n", n);
sort:
    for (int i = 0; i < n; i++)
    {
        if (array[i] > temp) temp = array[i];
    }
    count = 0;
    for (int i = 0; i < n; i++)
    {
        if (array[i] < temp && array[i] > 0) temp = array[i];
    }
    for(int i = 0; i < n; i++)
    {
        array[i] = array[i] - temp;
        if (array[i] > 0) count++;
    }
    if (count != 0) printf("%d\n", count);
    if (count >= 2) goto sort;
    return 0;
}


HackerRank Cut the sticks Solution in Java

import java.util.Scanner;

public class Cut_the_sticks {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        int[] sticks = new int[1000];
        for (int i = 0; i < N; i++) {
            int stickLen = input.nextInt();
            sticks[stickLen]++;
        }
        int remainingSticks = N;
        System.out.println(remainingSticks);
        for (int i = 0; i < sticks.length; i++) {
            if (sticks[i] > 0) {
                remainingSticks -= sticks[i];
                if (remainingSticks == 0) break;
                System.out.println(remainingSticks);
            }
        }
    }
}





Next Post Previous Post
No Comment
Add Comment
comment url