Ice Cream Parlor – HackerRank Problem Solution

Question

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 m=6 to spend and there are flavors costing cost= [1,3,4,5,6] . The two flavors costing 1 and 5 meet the criteria. Using -based indexing, they are at indices 1 and 4.

Input:

4

1 4 5 3 2

4
2 2 4 3

Output

1 4

1 2

Here is original problem on HackerRank

Answer:

Solution Logic

  • We have divided our solution in two part first try to find answer without loop if array contains 2 value of money /2
  • Iterate loop until won’t find the combination of two elements from an array which sum is equals two money.

Leave a Reply

Your email address will not be published. Required fields are marked *