Search This Blog

Saturday, September 28, 2013

Algorithm - Sorting - Counting Sort 2

Counting Sort 2

The following is the solution to Hacker Rank Problem "Counting Sort 2" using Java. For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 2/2
/**
 *
 */
package hackerRank;

import java.util.Scanner;

/**
 * @author Arun.G
 *
 */
public class CountingSort2 {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int numberCount[] = new int[100];
        //track the number count
        for(int i=0;i<n;i++)
        {
            int number = sc.nextInt();
            numberCount[number]+=1;
        }
        //print numbers for which count is greater than zero
        for(int i=0;i<numberCount.length;i++)
        {
            for(int j=0;j<numberCount[i];j++)
                System.out.print((i+1)+" ");
        }
       }

}


No comments:

Post a Comment

Labels