I have started learning Java recently and I solved the following problem on Hackerrank.
The challenge is to :
You are given NN sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick.
Suppose we have six sticks of the following lengths: 5 4 4 2 2 8
Then, in one cut operation we make a cut of length 2 from each of the six >sticks. For the next cut operation four sticks are left (of non-zero length), >whose lengths are the following: 3 2 2 6
The above step is repeated until no sticks are left.
Given the length of NN sticks, print the number of sticks that are left before each subsequent cut operations.
Note: For each cut operation, you have to recalcuate the length of smallest sticks (excluding zero-length sticks).
Input Format The first line contains a single integer NN. The next line contains NN integers: a0, a1,...aN-1 separated by space, where ai represents the length of ith stick.
Output Format For each operation, print the number of sticks that are cut, on separate lines.
Sample Input :
6
5 4 4 2 2 8
Sample Output:
6
4
2
1
I am trying to get better at writing good code for my solutions. Please give me your suggestions.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CutSticks {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int size = keyboard.nextInt(); // get total number of sticks
ArrayList<Integer> list = new ArrayList<Integer>(size);
for(int i=0;i<size;i++)
{
int values = keyboard.nextInt();
list.add(values);
}
list.removeAll(Collections.singleton(0));// remove all zeroes if present
Collections.sort(list); // sort in ascending order
while(list.size()>0)
{
int smallest = list.get(0); // get the smallest element
for(int i =0 ;i < list.size();i++)
{
list.set(i, list.get(i) - smallest);
}
//System.out.println(list);
System.out.println(list.size());
list.removeAll(Collections.singleton(0)); // remove the elements which are zero after cutting the sticks
//System.out.println(list);
}
}
}