Buy Two Chocolates
To solve this problem, we need to find the two smallest prices in the prices array. It is better to sort the prices array in ascending order, then pick the first two elements, which represent the minimum prices.
Here is the code:
|
|
This function starts by sorting the prices list. Then it calculates the sum of the two smallest prices. If this sum is less than or equal to the money we have, we subtract the sum from the money to get the leftover amount and return it. If the sum of the two smallest prices is more than the money we have, we return the money as it is because we cannot buy two chocolates without going into debt.