1
\$\begingroup\$

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[0] + " " + result[1]);   
        }
    }
}

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.