The Full Counting Sort

  • + 8 comments

    This solution works better than the other solutions discussed here.

    import java.util.Scanner;

    public class Solution {

    public static void main(String[] args)
    {
        Scanner scan=new Scanner(System.in);
        int size=Integer.parseInt(scan.nextLine());
        StringBuffer[] st=new StringBuffer[100]; 
    
        for(int i=0;i<100;i++)
        {
            st[i]=new StringBuffer();
        }
    
        for(int i=0;i<size;i++)
        {
            String sts=scan.nextLine();
            String[] str=sts.split("[\\s]+");
            int k=Integer.parseInt(str[0]);
            String s;
            if(i<size/2)
                s="- ";
            else
                s=str[1]+" ";
            st[k]=st[k].append(s);
        }
    
        for(int i=0;i<100;i++)
        {
            System.out.print(st[i]);
        }
    }
    

    }

    • + 0 comments

      quite a elegant solution

    • + 1 comment

      should've used "int size=scan.nextInt();" instead

      • + 0 comments

        https://www.hackerrank.com/challenges/countingsort4/submissions/code/55469006

    • + 0 comments

      If anyone is looking for a solution. this is it.

    • + 0 comments

      best solution for this problem

    • + 0 comments

      pls explain @insane84

    • + 1 comment

      Wouldnt it be better to avoid creating all StringBuffers at the start, by checking if a StringBuffer in the Array is null and creating one if it is?

      • + 1 comment

        Yes, U can do that that sort of thing, but it would be a messy to firstly count all numbers and then create a discrete array of string buffers and also the space complexity may not be improved very much as x is small(x<=100) and u are also using an another array to store values which are not null.

        • + 0 comments

          What I meant is something like this. I am unsure if its better, considering the tradeoff is having to check some if statements just to avoid creating empty Buffers. I am unsure what the "price" for creating empty buffers is.. I am pretty sure this would be better for a large scale with lots of empty Buffers, but this is obviously not the case here.

          public static void main(String[] args) {

          Scanner scan=new Scanner(System.in);
          
          int size=Integer.parseInt(scan.nextLine());
          
          StringBuffer[] st=new StringBuffer[100]; 
          
          for(int i=0;i<size;i++)
          {
              String sts=scan.nextLine();
              String[] str=sts.split("[\\s]+");
              int k=Integer.parseInt(str[0]);
              String s;
              if(i<size/2)
                  s="- ";
              else
                  s=str[1]+" ";
              if(st[k] == null) st[k] = new StringBuffer();
              st[k].append(s);
          }
          
          for(int i=0;i<100;i++)
          {
              if(st[i] != null) System.out.print(st[i]);
          }
          

          }

    • + 1 comment

      I had the same logic as you , although i was using Arrays.fill(st, new StringBuffer) instead of the loop

      for(int i=0;i<100;i++) { st[i]=new StringBuffer(); }

      and i was getting wrong answer all the time. In the step st[k]=st[k].append(s); i was getting all the array elements changed instead of the one at index k. can anyone help me with this??????

      • + 0 comments

        what you are doing is creating a reference variable and passing it every element ,so the reference to every variable(st[k]) is same.Instead what you have to do is initialise each variable independently so that each of them have has their own reference of a StringBuffer variable and changing one might not affect other.

    • + 0 comments

      Thanks to your comment I don't have Time problem anymore thanks again.