Files

42 lines
1.3 KiB
Java
Raw Permalink Normal View History

2017-03-05 00:26:42 -06:00
// Author: Rodney Shaghoulian
2018-07-27 16:45:56 -07:00
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag
2017-03-05 00:26:42 -06:00
import java.util.Scanner;
import java.util.HashMap;
2017-03-18 22:17:22 -05:00
// Time Complexity: O(n)
// Space Complexity: O(n)
2017-03-05 00:26:42 -06:00
public class Solution {
2017-09-11 09:32:17 -07:00
public static void main(String[] args) {
2017-03-10 01:10:37 -06:00
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while (t-- > 0) {
int m = scan.nextInt();
int n = scan.nextInt();
2017-04-05 18:42:35 -05:00
int [] costs = new int[n];
2017-03-10 01:10:37 -06:00
for (int i = 0; i < n; i++) {
costs[i] = scan.nextInt();
}
buyIceCream(costs, m);
}
scan.close();
}
2017-03-05 00:26:42 -06:00
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" */
2017-04-05 18:42:35 -05:00
int otherCost = money - cost;
2017-03-05 00:26:42 -06:00
if (map.containsKey(otherCost)) {
System.out.println(map.get(otherCost) + " " + icecreamID);
}
2017-04-05 18:42:35 -05:00
map.putIfAbsent(cost, icecreamID);
2017-03-05 00:26:42 -06:00
}
}
}