Solving the Ice Cream Parlor Problem | Hackerrank

Author: neptune | 04th-Jun-2023

Problem Statement: Two friends like to pool their money and go to the ice cream parlour. They always choose two distinct flavours and they spend all of their money.

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

Example: 

m = 6 cost= [1, 3, 4, 5, 6]


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 flavour of ice cream

Returns

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


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


Explanation

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

  1. The first time, they pool together m=4 dollars. Of the five flavours available that day, flavours 1 and 4 have a total cost of 1+3=4.

  2. The second time, they pool together m=4  dollars. Of the four flavours available that day, flavours 1 and 2 have a total cost of 2+2=4.


Solution in Python

The Ice Cream Parlor problem requires us to find two distinct flavours of ice cream that can be purchased with a given amount of money.


To solve this problem, we can use a simple algorithm that iterates through the list of prices and keeps track of the complements of each price. The complement of a price is the difference between the total amount of money and the current price. By storing the complements in a dictionary or a hash set, we can quickly check if the complement of the current price exists in the set.


Here's a step-by-step approach to solving the Ice Cream Parlor problem:


1. Create an empty dictionary or hash set to store the complements.


2. Iterate through the list of prices, starting from the first flavour.


3. For each flavour, calculate its complement by subtracting the current price from the total amount of money.


4. Check if the complement exists in the dictionary or hash set. If it does, we have found the two flavours that add up to the desired amount of money.


5. If the complement does not exist, add the current price to the dictionary or hash set for future lookups.


6. Return the indices of the two flavours that add up to the desired amount of money.


Now that we have outlined the approach, let's implement the `icecreamParlor` function in Python to solve the problem:


def icecreamParlor(m, arr):

    # Create a dictionary to store complements

    complement_dict = {}


    # Iterate through the list of prices

    for i, price in enumerate(arr):

        complement = m - price

        if complement in complement_dict:

            return [complement_dict[complement], i+1]

        else:

            complement_dict[price] = i+1


    return []


# Testing the function with the provided sample input

trips = 2

trip1_m = 4

trip1_flavors = [1, 4, 5, 3, 2]

trip2_m = 4

trip2_flavors = [2, 2, 4, 3]


print(icecreamParlor(trip1_m, trip1_flavors))  # Output: [1, 4]

print(icecreamParlor(trip2_m, trip2_flavors))  # Output: [1, 2]



In the above code, we define the `icecreamParlor` function that takes the amount of money (`m`) and the list of flavour prices (`arr`) as input. The function uses a dictionary (`complement_dict`) to store the complements of each price. It iterates through the list of prices, calculates the complement, and checks if it exists in the dictionary. If a complement is found, it returns the indices of the two flavours that add up to the desired amount of money. If no solution is found, it returns an empty list.


By following this approach and implementing the `icecreamParlor` function accordingly, we can efficiently solve the Ice Cream Parlor problem and find the indices of the flavours that satisfy the given conditions.




👉 Read More
The Blunder | Hackerrank
5. Solution of Hacker Rank Weather Observation Station 8.
7.Solution of Hacker Rank The Report
Identifying the Odd One Out in a Series of Strings | Hackerrank
4. Solution of Hacker Rank Weather Observation Station 6.
3. Solution of Hacker Rank Weather Observation Station 4.
The PADS | Hackerrank
6. Solution of Hacker Rank Employee Salaries.
1. Basic SQL Select Query of Hacker Rank.
Generate Fibonacci Sequence - JavaScript | Hackerank
Team Formation Hackerrank | Julia
Modified 0-1 knapsack problem | Frsco Play Hackerrank
2. Solution of Hacker Rank Weather Observation Station 3.
Weather Observation Station 18 | Hackerrank
AngularJS - Know Tech Frameworks | Fresco Play
PySpark Milestone Black Friday Sales Data | Fresco Play Hackerrank
Backspace String Compare using R | Fresco Play
Top Earners | HackerRank
Git - Recovering Discarded Changes
Python - Number Based Problem | Hackerrank
Explore more Blogs...