Search This Blog

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 !

Algorithms - Math - Find Digits

Find Digits

The following is the solution to Hacker Rank problem Find Digits 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 digit 1 always divides the number so we can increase the count to 1 always.
  2. All other digits should exactly divide the number, so it should have a remainder 0. So if remainder is zero increasing the count and 
  3. also we should note that the digit should not be zero.
Score: 5/5
import java.util.Scanner;

/**
 * @author Arun.G
 *
 */

public class Solution
    {
    public static int findDigits(int num)
        {
        int res=0;
        int number = num;
        String numbers = String.valueOf(num);
      
       
        for(int i=0;i<numbers.length();i++)
            {
            int digit = Integer.parseInt(Character.toString(numbers.charAt(i)));
            if(digit==1)
                res++;
            else if(digit>0 && number%digit==0)
                res++;
        }
        return res;
    }
    public static void main(String[] args)
        {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i=0;i<T;i++)
            {
            int num = sc.nextInt();
            System.out.println(findDigits(num));
              
        }
        sc.close();
    }
}


your Comments are welcome !

Saturday, April 19, 2014

Algorithms - Discrete Mathematics - Minimum Draws

Minimum Draws

The following is the solution to Hacker Rank problem Minimum Draws 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.InputStreamReader;

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

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

       //we need minimum of (noOfPairs+1) socks to get a matched pair of socks
       public static int minimumDraw(int noOfPairs)
      {
      return (noOfPairs+1);
    }
       public static void main(String[] args) throws NumberFormatException,
                     Exception {
             
              int T = Integer.parseInt(in.readLine());
        //T test cases follow
        for(int i=0;i<T;i++)
          {
          int noOfPairs = Integer.parseInt(in.readLine());
          System.out.println(minimumDraw(noOfPairs));
        }
       
        in.close();
             
             

       }
}

Algorithms - Discrete Mathematics - Handshake

Handshake

The following is the solution to Hacker Rank problem Handshake 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.InputStreamReader;

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

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

       public static void main(String[] args) throws NumberFormatException,
                     Exception {
             
              int T = Integer.parseInt(in.readLine());
       
        for(int i=0;i<T;i++)
          {
          int numofPersons = Integer.parseInt(in.readLine());
          System.out.println((int)(numofPersons*(numofPersons-1))/2);
        }
       
        in.close();
             
             

       }
}

Wednesday, April 16, 2014

Algorithms - Search- Missing Numbers

Missing Numbers

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

I'm using the following approach:
 since the two lists are sorted I'm comparing element in array  A with element in array B of the same index.

203 205
203 204 205

If they are same, moving to next index as they are not missing numbers.
203 205
      ▲
203 204 205
      ▲

if the elements are not same, it means it is a missing number, so adding 204 to list and moving B's index to next.
203 205
      ▲
203 204 205
              ▲

Again if it is same move to next index else add the missing numbers. If the A array length is reached all the elements now in B after the current index are missing so all of these are missing numbers.

Sort the missing list and display and don't forget the list should not have duplicates.
Score:40/40
/**
 *
 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

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

       public static void main(String[] args) throws NumberFormatException,
                     Exception {
              /*
               * Enter your code here. Read input from STDIN. Print output to STDOUT.
               * Your class should be named Solution.
               */

              int n = Integer.parseInt(in.readLine());
              List<Integer> A = new ArrayList<Integer>();
              List<Integer> B = new ArrayList<Integer>();
              ArrayList<Integer> missingNumbersList = new ArrayList<Integer>();
              //get all the numbers in A list
              String[] ANumbers = in.readLine().split(" ");
              for (int i = 0; i < ANumbers.length; i++) {
                     A.add(Integer.parseInt(ANumbers[i]));
              }
              //sort the A List
              Collections.sort(A);
              //get all the numbers in B list
              int m = Integer.parseInt(in.readLine());
              String[] BNumbers = in.readLine().split(" ");
              for (int i = 0; i < BNumbers.length; i++) {
                     B.add(Integer.parseInt(BNumbers[i]));

              }
              //sort the A List
              Collections.sort(B);
             
              int i = 0, j = 0;
              while (i < B.size()) {
                     if (j < A.size()) {
                           int numB = B.get(i);
                           int numA = A.get(j);
                           if (numA == numB) {
                                  i++;
                                  j++;
                           } else {
                                  if (!missingNumbersList.contains(numB))
                                         missingNumbersList.add(numB);
                                  i++;
                           }
                     } else {
                           if (!missingNumbersList.contains(B.get(i)))
                                  missingNumbersList.add(B.get(i));
                           i++;
                     }

              }

              Collections.sort(missingNumbersList);

              for (Integer num : missingNumbersList) {
                     System.out.print(num + " ");
              }

       }
}


Labels