Solving the Ice Cream Parlor Problem | Hackerrank

Author: neptune | 04th-Jun-2023
#Hackerrank #Problem Solving

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.





Related Blogs
5. Solution of Hacker Rank Weather Observation Station 8.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates...

The Blunder | Hackerrank
Author: neptune | 21st-Nov-2022
#SQL #Hackerrank
Write a query calculating the amount of error (i.e.: average monthly salaries), and round it up to the next integer...

7.Solution of Hacker Rank The Report
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8...

4. Solution of Hacker Rank Weather Observation Station 6.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates...

3. Solution of Hacker Rank Weather Observation Station 4.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table...

The PADS | Hackerrank
Author: neptune | 21st-Nov-2022
#SQL #Hackerrank
Problem Statement: Generate the following two result sets: 1. Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession...

6. Solution of Hacker Rank Employee Salaries.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : Query that prints a list of employee names for employees in Employee having a salary greater than $2000 per month and experience less than 10 months...

Identifying the Odd One Out in a Series of Strings | Hackerrank
Author: neptune | 15th-Jun-2023
#Hackerrank #Problem Solving
The article presents an algorithm to identify the odd one out in a series of strings efficiently...

1. Basic SQL Select Query of Hacker Rank.
Author: neptune | 20th-Apr-2022
#SQL #Hackerrank
Problem Statement : Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA...

2. Solution of Hacker Rank Weather Observation Station 3.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer...

Modified 0-1 knapsack problem | Frsco Play Hackerrank
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
An automobile mechanic wants to buy a set of spare parts from a manufacturing unit. Goal is to maximise the amount of money the mechanic can earn...

AngularJS - Know Tech Frameworks | Fresco Play
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
Build an application that displays the framework details using AngularJS routing. We will build an application that displays the Tech Frontend and Backend frameworks...

Backspace String Compare using R | Fresco Play
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
The code implementation in both R and Python solves the "Backspace String Compare" problem using stack data structure...

Git - Recovering Discarded Changes
Author: neptune | 13th-Jul-2023
#Github #Problem Solving
We will follow a scenario where a developer is working on a web application and needs to recover a discarded commit to reintroduce a specific feature...

PySpark Milestone Black Friday Sales Data | Fresco Play Hackerrank
Author: neptune | 05th-Nov-2023
#Data Science #Hackerrank
Welcome to the Spark Challenge. You are provided with the Black Friday sales data and we as a big data developer needs to analyze and fetch the required data...

Problem of Merging Two Sorted Linked Lists
Author: neptune | 02nd-Jun-2023
#Algorithms #Problem Solving
Merging two sorted linked lists efficiently into a single, sorted list can be achieved using optimized algorithms...

Cassandra Products JSON | Fresco Play
Author: neptune | 29th-Oct-2023
#Hackerrank #Problem Solving
We'll go through a series of common tasks related to managing data in Cassandra...

Finding the Most Expensive Keyboard and USB Drive within a Budget | Hackerrank
Author: neptune | 05th-Jun-2023
#Hackerrank #Problem Solving
A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget...

Python - Number Based Problem | Hackerrank
Author: neptune | 17th-Aug-2023
#Hackerrank #Problem Solving
Determine whether the number in descending order is a prime or not. If the number is a prime, then print "Sorted Number is a prime number," otherwise, print "Sorted Number is not a prime number."..

Join Operations in R | Fresco Play
Author: neptune | 29th-Oct-2023
#Hackerrank #Problem Solving
Perform various join operations in R using two data sets, 'flights' and 'weather'...

View More