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
       }

}


Wednesday, January 1, 2014

Algorithm - Linked Lists - Insert a node at a specific position in a linked list

Insert a node at a specific position in a linked list

The following is the solution to Hacker Rank problem Insert a node at a specific position in a linked 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
Node* InsertNth(Node *head, int data, int position)
{
  // Complete this method only
  // Do not write main function.
 
    Node *newNode = new Node();
    newNode->data = data;
    newNode->next = NULL;
    Node *temp = head;
 
    if(position == 0)
    {
        newNode->next = head;
        head = newNode;
    }
    else
    {
        for(int i = 1; i < position; i++)
            temp = temp->next;
        newNode->next = temp->next;
        temp->next = newNode;
    }
    return head;
}

Algorithm - Linked Lists - Delete a node from a linked list

Delete a node from a linked list

The following is the solution to Hacker Rank problem Delete a node from a linked 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
Node* Delete(Node *head, int position)
{
    // Complete this method
    Node *temp = head;
    if(position == 0)
        head = head->next;
    else
    {
        for(int i = 1; i < position; i++)
            temp = temp->next;
        Node *del = temp->next;
        temp->next = del->next;
    }
    return head;
}

Algorithm - Linked Lists - Print the elements of a linked list in reverse

Print the elements of a linked list in reverse

The following is the solution to Hacker Rank problem Print the elements of a linked list in reverse 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
void ReversePrint(Node *head)
{

  if(head == NULL)
    return;
   
  // traverse the list
  ReversePrint(head->next);

//print the element
  printf("%d\n", head->data);
}

Friday, December 6, 2013

Algorithm - Search - Find the Median

Find the Median

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

Score: 10/10
/**
 *
 */


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

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

       /**
        * @param args
        */

       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 n = sc.nextInt();
              int[] ar = new int[n];
              for (int i = 0; i < n; i++) {
                     ar[i] = sc.nextInt();
              }
              Arrays.sort(ar);

              int median = ar[ar.length / 2];
              System.out.println(median);

              sc.close();
       }

}       

Labels