# Ice Cream Parlor

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, , 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 and may have the same cost

Input Format

The first line contains an integer, t, denoting the number of trips to the ice cream parlor. The 3t subsequent lines describe all of Sunny and Johnny's trips to the parlor; each trip is described as follows:

Output Format

Print two space-separated integers denoting the respective ID numbers for the 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 1 to n.

public class Solution {

private static int[] solve(int m, int []costs) {
int []indexes = new int[]{-1, -1};
Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < costs.length; i++) {
int cost = costs[i];
int diff = m- cost;
if (diff < 0) continue;
Integer index = map.get(diff);

if (index == null) {
map.put(cost, i + 1);
}
else {
indexes = new int[]{map.get(diff), i + 1};
break;
}
}
return indexes;
}

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int T = s.nextInt();
for (int j = 0; j < T; j++) {
int M = s.nextInt();
int N = s.nextInt();
int []costs = new int[N];
for (int i = 0; i < N; i++) {
costs[i] = s.nextInt();
}
int []result = solve(M, costs);
System.out.println(result + " " + result);
}
}
}


I was able to relate this problem to the pair sum and my solution the implements same.

You're not using this index here correctly.

Integer index = map.get(diff);

if (index == null) {
map.put(cost, i + 1);
}
else {
indexes = new int[]{map.get(diff), i + 1};
break;
}


Either you're only interested in checking if a value exists, in which case you should use containsKey(diff) like so:

if (!map.containsKey(diff)) {
map.put(cost, i + 1);
}
else {
indexes = new int[]{map.get(diff), i + 1};
break;
}


Or you could reuse the retrieved value for creating indexes, like so:

Integer index = map.get(diff);

if (index == null) {
map.put(cost, i + 1);
}
else {
indexes = new int[]{index, i + 1};
break;
}


int diff = m- cost;


This line is hard to understand without the context of the problem statement. I'd recommend you pick a better name for m - targetSum might be a good indication.

Similarly, you say that you spotted that you can solve this problem with a pair sum. I think you should put that in the method name - contrast int[] solve(int m, int []costs) with int[] findPairSumIndices(int targetSum, int[] costs).

Another thing that bothers me, but now we're getting in pretty subjective territory, is the inconsistency in the placing of the array type.

private static int[] solve(int m, int []costs) {
int []indexes = new int[]{-1, -1};
indexes = new int[]{map.get(diff), i + 1};

public static void main(String[] args) {
int []costs = new int[N];
int []result = solve(M, costs);


In your code, return types and new array instantiations have the brackets next to the type, mainly because there's nowhere else for them to go. Variable declarations and method arguments have the brackets next to the variable name, except for the main method, which puts them next to the type again.

Personally, I'd say you should put the brackets next to the type and not the variable name. That way you're consistent throughout.
{"ft_lang_label":"__label__en","ft_lang_prob":0.82299495,"math_prob":0.96496964,"size":2112,"snap":"2019-43-2019-47","text_gpt3_token_len":548,"char_repetition_ratio":0.10531309,"word_repetition_ratio":0.025641026,"special_character_ratio":0.28361744,"punctuation_ratio":0.1463964,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9828514,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-20T13:30:04Z\",\"WARC-Record-ID\":\"<urn:uuid:eb4ee811-0640-441f-b925-b66f5c382b04>\",\"Content-Length\":\"137568\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:af954f47-7314-4104-8524-498a3444080f>\",\"WARC-Concurrent-To\":\"<urn:uuid:035d3048-b957-45ff-96be-f25c3a675653>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/151133/ice-cream-parlor/160179\",\"WARC-Payload-Digest\":\"sha1:ZFETOTSCY5SNJUWWP2MLAR62TPD6SPHE\",\"WARC-Block-Digest\":\"sha1:O5FRJBHNVCJDN3MCDC3DTZYKUHWOY3ZR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496670558.91_warc_CC-MAIN-20191120111249-20191120135249-00141.warc.gz\"}"}