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.
m = 6 cost= [1, 3, 4, 5, 6]
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
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
Sunny and Johnny make the following two trips to the parlour:
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.
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.