Search This Blog

Sunday, April 27, 2014

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 !

No comments:

Post a Comment

Labels