-
Notifications
You must be signed in to change notification settings - Fork 5
/
HashTableIceCreamParlour.java
49 lines (42 loc) · 1.57 KB
/
HashTableIceCreamParlour.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
// https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem
package search;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HashTableIceCreamParlour {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int queries = scanner.nextInt();
while (queries-- > 0) {
int money = scanner.nextInt();
int length = scanner.nextInt();
int[] array = getArray(length);
printIndices(array, money);
}
}
private static int[] getArray(int length) {
int[] array = new int[length];
for (int index = 0 ; index < array.length ; index++) {
array[index] = scanner.nextInt();
}
return array;
}
private static void printIndices(int[] array, int money) {
Map<Integer, Integer> indicesMap = getIndicesMap(array);
for (int index = 0 ; index < array.length ; index++) {
int element = array[index];
int difference = money - element;
if (indicesMap.containsKey(difference) && indicesMap.get(difference) != index + 1) {
System.out.println(index + 1 + " " + indicesMap.get(difference));
return;
}
}
}
private static Map<Integer, Integer> getIndicesMap(int[] array) {
Map<Integer, Integer> result = new HashMap<>();
for (int index = 0 ; index < array.length ; index++) {
result.put(array[index], index + 1);
}
return result;
}
}