Problem Statement
This is a challenge from HackerRank:
You are given N sticks, where each stick has the length of 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 N sticks, print the number of sticks that are cut in subsequent cut operations.
Input Format
The first line contains a single integer \$N\$. The next line contains \$N\$ integers: \$a_0, a_1,\ldots, a_{N-1}\$ separated by space, where \$a_i\$ represents the length of \$i^{th}\$ stick.
Output Format
For each operation, print the number of sticks that are cut in separate line.
Constraints: \$1 ≤ N ≤ 1000\$ and \$1 ≤ a_i ≤ 1000\$
Sample Input
6 5 4 4 2 2 8
Sample Output
6 4 2 1
Here is the code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
int a[n-1];
for(int i=0;i<n;++i)
{
int stick;
scanf("%d",&stick);
a[i]=stick;
}
int f=0;
do
{
int count=0,small=99;
f=0;
for(int i=0;i<n;++i)
{
if(a[i]>0&&a[i]<small)
small=a[i];
}
for(int i=0;i<n;++i)
{
if(a[i]!=0)
{
a[i]=a[i]-small;
++count;
f=1;
}
}
if(count)
printf("%d\n",count);
}while(f==1);
return 0;
}
How can I improve the above code?