Search This Blog

Friday, March 13, 2015

Algorithms - Warmup - Angry Professor

Angry Professor

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

It is a easy problem, the solution followed is simple. We know that if number of students in class is not equal to K we cancel the class else the class is not cancelled.

Score: 20/20

/**
 *
 */

import java.util.Scanner;

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

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
               Scanner sc = new Scanner(System.in);
               //no of test cases
               int T = Integer.parseInt(sc.nextLine());
            
               for(int count=0;count<T;count++)
                   {
                       String[] constraints = sc.nextLine().split(" ");
                       //n and k
                   int N =Integer.parseInt(constraints[0]);
                   int K =Integer.parseInt(constraints[1]);
                   //count no of students inside the class
                   int noOfStuds =0;
                   //all the students times are stored in string array
                   String[] students = sc.nextLine().split(" ");
                  
                   for(int i=0;i<N;i++)
                       {
                       int stud = Integer.parseInt(students[i]);
                       // if it is negative or zero the student arrived before the class
                       if(stud<=0)
                           noOfStuds++;
                       //k students are present in the class so no need to cancel the class, so NO
                       if(noOfStuds>=K)
                           {
                           System.out.println("NO");
                           break;
                       }
                   }
                   // if K students are not there in the class the class is cancelled so YES
                   if(noOfStuds<K)
                       System.out.println("YES");
               }
              
               sc.close();
        
       }

}

Tuesday, November 18, 2014

Hacker Rank - Weekly Challenges - Week 12

Hacker Rank - Weekly Challenges - Week 12 

Weekly Challenges are contests hosted by Hackerrank. We are given some set of problems to solve. You can see more about weekly challenges here.

Currently one problem is available to solve for this week.


  1. Priyanka and Toys
Following is the solution to the problem.

package hackerRank;

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class PriyankaAndToys
{
       public static void main (String[] args) throws java.lang.Exception
       {
              // your code goes here
              Scanner sc = new Scanner(System.in);
              int n = sc.nextInt();
              int count=1; int c =0;
              int[] array = new int[n];
              //aray
              for(int i=0;i<n;i++)
                     array[i] = sc.nextInt();
                    
              Arrays.sort(array);
              if(array.length >1)
              {
                     //constraint
                     c = array[0];
                    
                     for(int i=1;i<n;i++)
                     {
                           if(array[i]>=c && array[i]<=c+4 )
                                  continue;
                           else
                                  count++;
                                  c = array[i];
                     }
              }
              else if(array.length==1)
                     count =1;
              System.out.println(count);
              sc.close();
       }
}

Monday, November 10, 2014

Algorithms - Warmup - Alternating Characters

Alternating Characters

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

It is a easy problem, one solution is to read all characters one by one and check if both are equal. If both these characters are equal, we keep count of the character. This count will give us the number of characters to be deleted.

Score:30/30

/**
 *
 */

import java.util.Scanner;

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

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub

              Scanner sc = new Scanner(System.in);

              int T = sc.nextInt();
              for (int count = 0; count < T; count++) {
                     char[] characters= sc.next().toCharArray();
                      int AlterCount=0;
                     for(int i=0;i<characters.length-1;i++)
                     {
                           if(characters[i]==characters[i+1])
                           {
                                  AlterCount++;
                           }
                          
                     }
                     System.out.println(AlterCount);
                    
              }

              sc.close();
       }

}

Thursday, September 4, 2014

Algorithms - Dynamic Programming - Stock Maximize

Stock Maximize.

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

Score: 36/36

import java.util.Scanner;

public class StockMaximize {

       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);
              int numOfTestCase = sc.nextInt();
              for (int i = 0; i < numOfTestCase; i++) {
                     int n = sc.nextInt();
                     long profit = 0;
                     int[] stockPrice = new int[n];

                     for (int j = 0; j < n; j++) {
                           stockPrice[j] = sc.nextInt();
                     }
                    
                     int currMax = Integer.MIN_VALUE;
                    
                     for (int j = n - 1; j >= 0; j--) {
                           if (currMax < stockPrice[j]) {
                                  currMax = stockPrice[j];
                           }
                           profit += (currMax - stockPrice[j]);
                     }
                     System.out.println(profit);
                    
                     sc.close();
              }
       }
}

Algorithms - Combinatorics - Building a List

Building a List

The following is the solution to Hacker Rank problem Building a List using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 5/5


/**
 *
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * @author Arun.G
 *
 */
public class Solution {
       BufferedReader reader;
       StringTokenizer tokenizer;
       PrintWriter out;
       private StringBuilder output = new StringBuilder();
       String inputstring = "";

      
       private void combine(int start) {
              for (int i = start; i < inputstring.length(); ++i) {
                     output.append(inputstring.charAt(i));

                     if (!output.equals(""))
                           System.out.println(output);

                     if (i < inputstring.length())
                           combine(i + 1);
                     output.setLength(output.length() - 1);
              }
       }

       public void solve() throws IOException {

              int T = nextInt();

              for (int count = 0; count < T; count++) {
                     nextInt(); // to get N value, but it is used
                     inputstring = nextToken();
                     combine(0);
              }
       }

       /**
        * @param args
        */
       public static void main(String[] args) {
              new Solution().run();
       }

       public void run() {
              try {
                     reader = new BufferedReader(new InputStreamReader(System.in));
                     tokenizer = null;
                     out = new PrintWriter(System.out);
                     solve();
                     reader.close();
                     out.close();
              } catch (Exception e) {
                     e.printStackTrace();
                     System.exit(1);
              }
       }

       int nextInt() throws IOException {
              return Integer.parseInt(nextToken());
       }

       long nextLong() throws IOException {
              return Long.parseLong(nextToken());
       }

       double nextDouble() throws IOException {
              return Double.parseDouble(nextToken());
       }

       String nextToken() throws IOException {
              while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                     tokenizer = new StringTokenizer(reader.readLine());
              }
              return tokenizer.nextToken();
       }
}


Labels