Search This Blog

Sunday, May 11, 2014

Algorithms - Discrete Mathematics - K Candy Store

K Candy Store

The following is the solution to Hacker Rank problem K Candy Store 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.math.BigInteger;
import java.util.StringTokenizer;

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

       BufferedReader reader;
       StringTokenizer tokenizer;
       PrintWriter out;

       public BigInteger factorial(BigInteger  n)
       {
              if(n.compareTo(BigInteger.ONE)==0 
                         || n.compareTo(BigInteger.ZERO)==0)
                     return BigInteger.ONE;
              else
                     return (n.multiply(
                        factorial(n.subtract(BigInteger.ONE))
                        ));
       }
       public void solve() throws IOException {
              int T = nextInt();
              for (int count = 0; count < T; count++) {
                     int N = nextInt();
                     int K = nextInt();
                     BigInteger nCk = BigInteger.ONE;
                     nCk = factorial(new BigInteger((N+K-1)+"")).divide(factorial(new BigInteger(K+"")).multiply(factorial( BigInteger.valueOf(N-1))));
                    
                    if(nCk.toString().length()>9)
                     {
                      System.out.println(new BigInteger(nCk.toString().substring(nCk.toString().length()-9,nCk.toString().length())));
                     }
                     else
                     {
                     System.out.println(nCk);
                     }
                    
              }
       }

       /**
        * @param args
        */
       public static void main(String[] args) {
              new KCandyStore().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();
       }
}

Algorithms - Discrete Mathematics - nCr table

nCr table

The following is the solution to Hacker Rank problem nCr Table 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.math.BigInteger;
import java.util.StringTokenizer;

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

       BufferedReader reader;
       StringTokenizer tokenizer;
       PrintWriter out;

       public void solve() throws IOException {
              int T = nextInt();
              for (int count = 0; count < T; count++) {
                     int N = nextInt();

                     BigInteger nCk = BigInteger.ONE;
                     int i=N;
                           for (int j = 0; j <= N; j++) {
                                  System.out.print(nCk.mod(new BigInteger("1000000000")) + " ");
                                  nCk = nCk.multiply(new BigInteger((i - j)+"")).divide(new BigInteger(((j + 1))+""));
                           }
                           System.out.println();
                    
              }

       }

       /**
        * @param args
        */
       public static void main(String[] args) {
              new nCrtable().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();
       }
}

Sunday, April 27, 2014

Algorithms - Discrete Mathematics - Diwali Lights

Diwali Lights

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

The Approach I'm following is given below:
  1. The problem is easy, if we have 2 bulbs atleast 1 bulb should be set at any time so there are 22-1 possibilities eliminating the condition where all the bulbs are turned off.
  2. So if we have N bulbs then we have 2N-1 possibilities which is large  number to compute.
  3. We can do this easily with Java's BigInteger , we can calculate the power using pow function.
  4. Also we must note since this number is large we are asked to print the result mod 105 we can also do this easily with Java's BigInteger mod function.
Score: 5/5
/**
 *
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

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

       /**
        * @param args
        */
       static BufferedReader in = new BufferedReader(new InputStreamReader(
                     System.in));

       public static BigInteger ExponentBySquare(long x, long n)
    {
              BigInteger big = BigInteger.valueOf(x)
                   .pow((int)n)
                   .subtract(BigInteger.ONE).mod(new BigInteger("100000"));
             
              return big;
    }
       public static void main(String[] args) throws NumberFormatException,
                     Exception {
             
              int T = Integer.parseInt(in.readLine());
       
        for(int i=0;i<T;i++)
          {
          long noOfBulbs = Long.parseLong(in.readLine());
          System.out.println(ExponentBySquare(2,noOfBulbs).toString());
        }
       
        in.close();
             
             

       }
}


Your Comments are Welcome !

Algorithms - Math - Ice Cream Parlor

Ice Cream Parlor

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

The Approach I'm following is given below:


  1. I have used easy approach, i.e a Complete Search to find if any two items can be bought for given cost and print the indexes.
  2. The indexes in the answer starts from 1 to N instead of 0 to N-1;
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 k = 0; k < T; k++) {
                     int M = sc.nextInt();
                     int N = sc.nextInt();

                     int[] Cost = new int[N];
                     // get the cost
                     for (int index = 0; index < N; index++)
                           Cost[index] = sc.nextInt();

                     for (int i = 0; i < N; i++) {
                           for (int j = i; j < N; j++) {
                                  if (i == j)
                                         continue;
                                  else {
                                         if (Cost[i] + Cost[j] > M)
                                                continue;
                                         else if (Cost[i] + Cost[j] == M) {
                                         System.out.println((i+1) + " " + (j+1));
                                         }
                                  }
                           }

                     }

              }
              sc.close();
       }

}


Your Comments are Welcome !

Labels