Ice Cream Parlor HackerRank Solution

Ice Cream Parlor HackerRank Solution

In this Ice Cream Parlor HackerRank solution, Two friends like to pool their money and go to the ice cream parlor. They always choose two distinct flavors and they spend all of their money.

Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have.


The two flavors that cost  and  meet the criteria. Using -based indexing, they are at indices  and .

Function Description

Complete the icecreamParlor function in the editor below.

icecreamParlor has the following parameter(s):

  • int m: the amount of money they have to spend
  • int cost[n]: the cost of each flavor of ice cream


  • int[2]: the indices of the prices of the two flavors they buy, sorted ascending

Input Format

The first line contains an integer, , the number of trips to the ice cream parlor. The next  sets of lines each describe a visit.

Each trip is described as follows:

  1. The integer , the amount of money they have pooled.
  2. The integer , the number of flavors offered at the time.
  3.  space-separated integers denoting the cost of each flavor: .

Note: The index within the cost array represents the flavor of the ice cream purchased.


  • , ∀ 
  • There will always be a unique solution.

Sample Input

STDIN       Function
-----       --------
2           t = 2
4           k = 4
5           cost[] size n = 5
1 4 5 3 2   cost = [1, 4, 5, 3, 2]
4           k = 4
4           cost[] size n = 4
2 2 4 3     cost=[2, 2,4, 3]

Sample Output

1 4
1 2


Sunny and Johnny make the following two trips to the parlor:

  1. The first time, they pool together  dollars. Of the five flavors available that day, flavors  and  have a total cost of .
  2. The second time, they pool together  dollars. Of the four flavors available that day, flavors  and  have a total cost of .

Ice Cream Parlor HackerRank solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in Python

def icecreamParlor(m, arr):
    # Write your code here
    dict_prices = {}
    for index in range(0, len(arr)):
        price = arr[index]
        if m - price in dict_prices:
            return [dict_prices[m-price]+1, index+1]
            dict_prices[price] = index

Problem Solution in JavaScript

function icecreamParlor(m, arr) {
    // Write your code here
    let map=new Map();
    for(let i=0;i<arr.length;i++){
        let i2=m-arr[i];
            return [map.get(i2),i+1].sort((a,b)=>a-b);

Problem Solution in C#

public static List<int> icecreamParlor(int m, List<int> arr)
        for (int i = 0; i < arr.Count - 1; i++) {
            int price1 = arr[i];
            if (price1 < m) {
                int price2 = m - price1;
                int index = arr.FindIndex(i + 1, p => p == price2);
                if (index > -1) {
                    return new List<int>() {i + 1, index + 1};
        return null;

Problem Solution in Java

public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
    HashSet<Integer> set = new HashSet<>();
    ArrayList<Integer> list= new ArrayList<>();
    for(int i =0; i < arr.size(); i++){
    for (int i = 0; i < arr.size(); i++) {
          if (set.contains(m - arr.get(i)) && (arr.indexOf(m - arr.get(i)) != i)) {
               list.add(i + 1);
               list.add(arr.indexOf(m - arr.get(i)) + 1);
    return list;
Solve original Problem on HackerRank here. Checkout more HackerRank Problems