Hackerrank - Ice Cream Parlor Solution
2 min read

Hackerrank - Ice Cream Parlor Solution

Hackerrank - Ice Cream Parlor Solution

Sunny and Johnny like to pool their money and go to the ice cream parlor. Johnny never buys the same flavor that Sunny does. The only other rule they have is that 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.

For example, they have  to spend and there are flavors costing . The two flavors costing  and  meet the criteria. Using -based indexing, they are at indices  and .

Function Description

Complete the icecreamParlor function in the editor below. It should return an array containing the indices of the prices of the two flavors they buy, sorted ascending.

icecreamParlor has the following parameter(s):

  • m: an integer denoting the amount of money they have to spend
  • cost: an integer array denoting the cost of each flavor of ice cream

Input Format

The first line contains an integer, , denoting 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.

Constraints

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

Output Format

For each test case, print two space-separated integers denoting the indices of the two flavors purchased, in ascending order.

Sample Input

2
4
5
1 4 5 3 2
4
4
2 2 4 3

Sample Output

1 4
1 2

Explanation

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 .

Solution in Python

from collections import Counter

def icecreamParlor(m, arr):
    costs = Counter(arr)
    half = m/2
    combos = set()
    for cost in costs:
        if (cost!=half and m-cost in costs) or (cost==half and costs[cost]>1):
            combos.add(cost)
    for index,cost in enumerate(arr,1):
        if cost in combos:
            yield index
for _ in range(int(input())):
    m,n = int(input()), int(input())
    arr = list(map(int,(input().split())))
    print(*icecreamParlor(m, arr))

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×