Search This Blog

Sunday, March 9, 2014

Algorithms - Warmup - Chocolate Feast

Chocolate Feast

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

Score: 20/20
/**
 *
 */


import java.util.Scanner;

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

       /**
        * @param args
        */
       //Number of chocolates we actually can buy using available money
       private static int numberOfChocolates(int N, int C) {
              return N / C;
       }

       //calculate how much chocolate we can buy with the wrappers
       private static int numberOfChocolatesForWrappers(int chocolates, int M) {
              int result = 0;
              while (chocolates / M > 0) {
                     result += chocolates / M; //chocolate we can get
                     //chocolates we can get + chocolate/ wrappers unused
                     chocolates = (chocolates / M) + (chocolates % M);
              }
              return result;
       }

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

              Scanner sc = new Scanner(System.in);
              int T = sc.nextInt();

              for (int i = 0; i < T; i++) {
                     int N = sc.nextInt();
                     int C = sc.nextInt();
                     int M = sc.nextInt();

                     int totChocolates = 0;
                     totChocolates = numberOfChocolates(N, C)
                                  + numberOfChocolatesForWrappers(numberOfChocolates(N, C), M);
                     System.out.println(totChocolates);
              }
              sc.close(); // close the scanner
       }

}

Tuesday, March 4, 2014

Algorithms - Warmup - Service Lane

Service Lane

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

Score:30/30
/**
 *
 */


import java.util.Scanner;

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

       /**
        * @param args
        */
       //function which returns the minimum element between indexes in an array
       public static int findMin(int[] array, int startIndex, int endIndex)
    {
    int min = Integer.MAX_VALUE;
    for(int i = startIndex;i<=endIndex;i++)
        {
        if(array[i]<min)
            min = array[i];
    }
    return min;
}
       public static void main(String[] args) {
              // TODO Auto-generated method stub
               Scanner sc = new Scanner(System.in);
               int N = sc.nextInt();
               int T = sc.nextInt();
               //declare lane segment array to store width of n elements
               int[] laneSegments = new int[N];
               //get the width into array
               for(int i=0;i<N;i++)
                   {
                   laneSegments[i]= sc.nextInt();
               }
             
               //T Test Cases to handle
               for(int k=0;k<T;k++)
                   {
                   int i = sc.nextInt();
                   int j = sc.nextInt();
                   /*find the minimum of the lane segment width,
                    * that is the maximum size of the vehicle
                   that can pass through*/
                   System.out.println(findMin(laneSegments,i,j));
               }
               sc.close();
       }

}

Monday, March 3, 2014

Algorithms - Warmup - Utopian Tree

Utopian Tree

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


Score: 20/20
/**
 *
 */


import java.util.Scanner;

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

       /**
        * @param args
        */
       private static int UtopianHeight(int noOfCycles)
    {
    int height=1;
    if(noOfCycles<=0)
        return 1; // no change of height
    for(int i=1;i<=noOfCycles;i++)
        {
        if(i%2!=0)
            height=2*height; //doubles in odd cycles
        else
            height+=1; // increases by 1 in even cycles
    }
    return height;
}
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              /* 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 noOfTestCases = sc.nextInt();
        for(int i=0;i<noOfTestCases;i++)
            {
              int noOfCycles = sc.nextInt();
            System.out.println(UtopianHeight(noOfCycles));
        }
        sc.close(); //close scanner
       }

}


Labels