Buy Two Chocolates Problem

Buy Two Chocolates Problem — ExecCode Easy DSA Practice

Solve the Buy Two Chocolates problem on ExecCode. Free online easy DSA practice in Arrays - Logic Building. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Description You are given an integer array prices representing the prices of various chocolates in a store. Your task is to compute the required answer and return the amount of money you will have leftover after buying the two chocolates. You are also given a single integer money, which represents your initial amount of money. You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy. If there is no way for you to buy two chocolates without ending up in debt, return money.

Examples

Input {"prices": [1, 2, 2], "money": 3}; Output 0. Input {"prices": [3, 2, 3], "money": 3}; Output 3

Constraints

2 <= prices.length <= 50 1 <= prices[i] <= 100 1 <= money <= 100

Practice Buy Two Chocolates free on ExecCode. Browse DSA problems, topic map, and placement guides.