-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIceCreamParlor.java
75 lines (67 loc) · 2.58 KB
/
IceCreamParlor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Each time Sunny and Johnny take a trip to the Ice Cream Parlor,
they pool together m dollars for ice cream. On any given day,
the parlor offers a line of n flavors. Each flavor, i, is
numbered sequentially with a unique ID number from 1 to n and
has a cost, ci, associated with it.
Given the value of m and the cost of each flavor for t trips to
the Ice Cream Parlor, help Sunny and Johnny choose two flavors
such that they spend their entire pool of money (m) 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 i and j may have the
same cost (i.e., c1 == c2).
Link: https://www.hackerrank.com/challenges/ctci-ice-cream-parlor
*/
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];
HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
if (map.containsKey(a[a_i])) {
List<Integer> list = map.remove(a[a_i]);
list.add(a_i);
map.put(a[a_i], list);
}else {
List<Integer> list = new ArrayList<Integer>();;
list.add(a_i);
map.put(a[a_i], list);
}
}
Arrays.sort(a);
printTwoIds(a, map, m);
}
}
private static void printTwoIds(int[] a, HashMap<Integer, List<Integer>> map, int target) {
int N = a.length;
for (int i = 0; i < N; i++) {
int diff = target - a[i];
if (map.containsKey(diff)) {
int firstId = map.get(a[i]).get(0) + 1;
int secondId;
if (a[i] == diff)
secondId = map.get(a[i]).get(1) + 1;
else
secondId = map.get(diff).get(0) + 1;
if (firstId < secondId)
System.out.println(firstId + " " + secondId);
else
System.out.println(secondId + " " + firstId);
return;
}
}
}
}