Answer to Question #158954 in Java | JSP | JSF for charmi

Question #158954

alice and bob were buying chocolates.It was Alice turn today to buy Bob chocolates.Bob gives list A of N chocolates where A[i] denotes price of ith chocolate.Alice being on a tight budget asks Bob if he can remove a few chocolates.Bob allows him to remove some chocolate whose price is X(which Alice can decide).Please find the minimum amount alice has to pay today.​

1



1
Expert's answer
2021-01-27T09:35:39-0500
import java.util.HashMap;

public class Main {
    public static int minPrice(int[] prices) {
        int total = 0;
        HashMap<Integer, Integer> uniquePrices = new HashMap<>();
        for (int price : prices) {
            total += price;
            if (uniquePrices.get(price) != null) {
                uniquePrices.put(price, uniquePrices.get(price) + price);
            } else {
                uniquePrices.put(price, price);
            }
        }
        int min = total;
        for (Integer value : uniquePrices.values()) {
            min = Math.min(min, total - value);
        }
        return min;
    }

    public static void main(String[] args) {
        int[] prices = {1, 10, 4, 4, 8, 7, 1, 4, 1, 2, 0, 10};

        System.out.println(minPrice(prices));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog