Monday, 4 December 2017

Binary Search: Ice Cream Parlor Hacker Rank Problem Solution Using JAVA.

Each time Sunny and Johnny take a trip to the Ice Cream Parlor, they pool together  dollars for ice cream. On any given day, the parlor offers a line of  flavors. Each flavor, , is numbered sequentially with a unique ID number from  to  and has a cost, , associated with it.
Given the value of  and the cost of each flavor for  trips to the Ice Cream Parlor, help Sunny and Johnny choose two distinct flavors such that they spend their entire pool of money during each visit. For each trip to the parlor, print the ID numbers for the two types of ice cream that Sunny and Johnny purchase as two space-separated integers on a new line. You must print the smaller ID first and the larger ID second.
Note: Two ice creams having unique IDs  and  may have the same cost (i.e., ).
Input Format
The first line contains an integer, , denoting the number of trips to the ice cream parlor. The  subsequent lines describe all of Sunny and Johnny's trips to the parlor; each trip is described as follows:
  1. The first line contains .
  2. The second line contains .
  3. The third line contains  space-separated integers denoting the cost of each respective flavor. The  integer corresponds to the cost, , for the ice cream with ID number  (where ).
Constraints
  • , where 
  • It is guaranteed that there will always be a unique solution.
Output Format
Print two space-separated integers denoting the respective ID numbers for the two distinct flavors they choose to purchase, where the smaller ID is printed first and the larger ID is printed second. Recall that each ice cream flavor has a unique ID number in the inclusive range from  to .
Sample Input
2
4
5
1 4 5 3 2
4
4
2 2 4 3
Sample Output
1 4
1 2
Explanation
Sunny and Johnny make the following two trips to the parlor:
  1. The first time, they pool together  dollars. There are five flavors available that day and flavors  and  have a total cost of . Thus, we print 1 4 on a new line.
  2. The second time, they pool together  dollars. There are four flavors available that day and flavors  and have a total cost of . Thus, we print 1 2 on a new line.
Java Code :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            int m = in.nextInt();
            int n = in.nextInt();
            int a[] = new int[n];
            for(int a_i=0; a_i < n; a_i++){
                a[a_i] = in.nextInt();
            }
            for(int j=0;j<n;j++)
            {
                int counter =0;
                for(int z =j+1;z<n-1;z++)
                {
                   if(a[j]+a[z] == 4 )
                   {
                       j++;
                       z++;
                       System.out.println(j+" "+z);
                       counter++;
                       break;
                   }
                }
                if(counter == 1)
                {
                    break;
                }
 
            }

        }
    }
}

No comments:

Post a Comment

LCM of n numbers Using Java

Found it in Geeks For Geeks : https://www.geeksforgeeks.org/lcm-of-given-array-elements/ A good Solution to be Considered: // Java Pro...