info@meritcampus.com    +91-85006-22255
...

Given the available measuring weights, write a program to identify which weights should be placed in which pan so as to weigh the required amount of rice. If two or more combinations can be used to measure the given weight, we should give higher preference for the combination where the total of weights in pan 1 and pan 2 is lowest. Assume that rice is always placed in pan 2. If the required amount can not be weighed, then leave the lists empty.

Input (List, int) Output (List, List)
[1, 2, 6, 9, 100], 5 [6], [1]
[1, 4, 20, 40], 15 [20], [1, 4]
[1, 6, 4, 16, 20], 45 [4, 6, 16, 20], [1]
[2, 4, 4, 5, 8], 19 [2, 4, 5, 8], []
[1, 2, 4, 20, 40, 60], 94 [40, 60], [2, 4]
[1, 2, 4, 40, 80, 90], 100 [], []
[2, 1, 5, 6, 15, 20, 30, 45, 70], 149 [1, 2, 5, 6, 15, 20, 30, 70], []
[30, 4, 6, 20, 1, 120, 70], 199 [4, 6, 70, 120], [1]
[30, 100, 40, 6, 3], 164 [30, 40, 100], [6]
[123, 346, 67, 8, 76, 9, 7, 42, 24, 978, 908, 212], 200 [7, 8, 42, 67, 76], []
[1, 2, 7, 9, 12], 5 [7], [2]

``` class WeighRiceUsingPanBalance {    public static void main(String s[])    {        List availableWeights = new ArrayList();        availableWeights.add(1);        availableWeights.add(2);        availableWeights.add(7);        availableWeights.add(9);        availableWeights.add(12);        List pan1Weigths = new ArrayList();        List pan2Weigths = new ArrayList();        addWeightsToPans(availableWeights, 19, pan1Weigths, pan2Weigths);        System.out.println("Weights to be placed in Pan 1 are : " + pan1Weigths);        System.out.println("Weights to be placed in Pan 2 are : " + pan2Weigths);    } public static void addWeightsToPans(List<Integer> weights, int weight, List<Integer> pan1Weigths, List<Integer> pan2Weigths) { //Write code here to identify the currency notes which have to be returned for the dispensing the requested amount } //If required, write any additional methods here } ```

Topic: Java ArrayList

0
Wrong
Score more than 2 points