Search This Blog

Thursday, April 10, 2014

Algorithms - Warmup - Filling Jars

Filling Jars

The following is the solution to Hacker Rank problem Filling Jars 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.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

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

       /**
        * @param args
        */
       public static BigInteger noOfCandies(int a, int b, int k) {
              BigInteger noOfChocolates = BigInteger.ONE;
             
              long difference = (b-a+1);
       noOfChocolates = noOfChocolates.multiply(BigInteger.valueOf(difference));
              return noOfChocolates = noOfChocolates.multiply(BigInteger.valueOf(k));
       }

       public static void main(String[] args) {
              /*
               * Enter your code here. Read input from STDIN. Print output to STDOUT.
               * Your class should be named Solution.
               */
               try
               {
                     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             String[] line = br.readLine().split(" ");
                     int N = Integer.parseInt(line[0]);
                     int M = Integer.parseInt(line[1]);
                     BigInteger noOfCandy = BigInteger.ZERO;
               BigInteger average = BigInteger.ZERO;
                     // M Operations to handle
                     for (int i = 0; i < M; i++) {
                 String[] lines = br.readLine().split(" ");
                           int a = Integer.parseInt(lines[0]);
                           int b = Integer.parseInt(lines[1]);
                           int k = Integer.parseInt(lines[2]);
                           noOfCandy = noOfCandy.add(noOfCandies(a, b, k));

                     }
               average = noOfCandy.divide(BigInteger.valueOf(N));
                     System.out.println(average);
             }
             catch(Exception ex)
               {
               System.out.println(ex.toString());
             }
                    
       }

}

No comments:

Post a Comment

Labels