We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
i make array of 1001(1-1000) element and count occurence of each digit in given input.
then i print the first row of output which always going to be same as value of n
then i again start loop(0-1000) and whenever elemnt of array found non-zero(which is occurence of smallest element), i substract it from n and assign result to n again and print value as result.
According to me there is no need to substract lowest value from remaining sticks because whether you substract or not, overall effect on elements is going to be null!!....
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[1001];
for(int i=0;i<n;i++)
{
int x = sc.nextInt();
arr[x]++;
}
System.out.println(n);
for(int j=1;j<=1000;j++)
{
if(arr[j]!=0)
{
n=n-arr[j];
if(n!=0)
System.out.println(n);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cut the sticks
You are viewing a single comment's thread. Return to all comments →
JAVA O(N) Simple solution :)
Approach:
According to me there is no need to substract lowest value from remaining sticks because whether you substract or not, overall effect on elements is going to be null!!....