• + 3 comments

    JAVA O(N) Simple solution :)

    Approach:

    1. i make array of 1001(1-1000) element and count occurence of each digit in given input.
    2. then i print the first row of output which always going to be same as value of n
    3. 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);                
            }
        }