Search This Blog

Sunday, September 29, 2013

Algorithm - Sorting - Counting Sort 3

 Counting Sort 3
The following is the solution to Hacker Rank problem "Counting Sort 3" 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 CountingSort3 {

       /**
        * @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];
        // tracking number count
        for(int i=0;i<=n;i++)
        {
              String[] words =sc.nextLine().split(" ");
              if(!words[0].equals(""))
              {
            int number = Integer.parseInt(words[0]);
            numberCount[number]+=1;
              }
        }
        int sum=0;
        for(int i=0;i<numberCount.length;i++)
        {
            sum=0;
            for(int j=0;j<=i;j++)
               sum+=numberCount[j];
           
            System.out.print(sum+" ");
        }
        sc.close();

       }

}

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)+" ");
        }
       }

}


Algorithm - Sorting - Counting Sort 1

 Counting Sort 1
The following is the solution to Hacker Rank problem "Counting Sort 1" 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 CountingSort1 {

       /**
        * @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 number count
        for(int i=0;i<numberCount.length;i++)
        {
            System.out.print(numberCount[i]+" ");
        }
       }

}

Hacker Rank Problem - Algorithm - Sorting - Intro to Tutorial Challenges

Intro to Tutorial Challenges
The following is the solution to hacker rank problem "Intro to Tutorial Challenges" using Java. Visit my page hackerrank for solutions to other Hacker Rank problems.

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

import java.util.Scanner;

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

       /**
        * @param args
        */

       public static int findValueInArray(int[] ar, int value) {
              for (int i = 0; i < ar.length; i++) {
                     if (ar[i] == value)
                           return i;
              }
              return -1;
       }

       public static void main(String[] args) {
              // TODO Auto-generated method stub
              Scanner sc = new Scanner(System.in);

              int value = sc.nextInt();
              int n = sc.nextInt();
              int[] ar = new int[n];
              for (int i = 0; i < n; i++) {
                     ar[i] = sc.nextInt();
              }
              System.out.println(findValueInArray(ar, value));
       }

}


Sunday, September 22, 2013

Hacker Rank Problem - Regex - Detect the Email Addresses

Detect the Email Addresses

The following is the solution to the Hacker Rank problem "Detect the Email Addresses" using Java. For solutions to other hacker rank problems visit my page HackerRank.

Score: 15/15
/**
 *
 */
package hackerRank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

       /**
        * @param args
        */
       //for printing the email's separated by Semicolon
    public static void printList(ArrayList<String> list)
    {
        String result="";
        for(String email : list)
        {
            result+= email+";";
        }
        //remove the last semicolon
        result = result.substring(0,result.length()-1);
        System.out.println(result);
    }
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
         Scanner sc = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<String>();
        int no = sc.nextInt();
        while(no-->=0)
        {
            String word = sc.nextLine();
            if(!word.equals(""))
            {
                String emailPattern ="(\\b[a-z_.]{1,}@[a-z.]*[a-z]{1,3}\\b)";
                Pattern pattern = Pattern.compile(emailPattern,Pattern.CASE_INSENSITIVE);
                Matcher m = pattern.matcher(word);
                while(m.find())
                {
                    String emailAddress = m.group(1);
                    if(!list.contains(emailAddress))
                        list.add(emailAddress);
                }
                  
            }
        }
        //sorting for lexicographical order
        Collections.sort(list);
        //printing the result
        printList(list);
    }
}


Labels