-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution.java
42 lines (37 loc) · 1.25 KB
/
Solution.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
/*
* DeveloperName(): Jignesh Chudasama
* GithubName(): https://github.com/Jignesh-81726
*/
import java.util.Scanner;
import java.util.HashMap;
// Time Complexity: O(n)
// Space Complexity: O(n)
public class Solution {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while (t-- > 0) {
int m = scan.nextInt();
int n = scan.nextInt();
int [] costs = new int[n];
for (int i = 0; i < n; i++) {
costs[i] = scan.nextInt();
}
buyIceCream(costs, m);
}
scan.close();
}
public static void buyIceCream(int [] costs, int money) {
HashMap<Integer, Integer> map = new HashMap<>(); // key = cost, value = ice cream ID
for (int i = 0; i < costs.length; i++) {
int icecreamID = i + 1;
int cost = costs[i];
/* Find 2 flavors to buy that sum to "money" */
int otherCost = money - cost;
if (map.containsKey(otherCost)) {
System.out.println(map.get(otherCost) + " " + icecreamID);
}
map.putIfAbsent(cost, icecreamID);
}
}
}