Cooling Pies : COOLING

Code :

What is the error in my code?

After taking the input, the nested for loops are used to compare all the elements of array b (where I have stored the weights of the pies), with all elements of array c (where I have stored the eight limits of the racks). If the weight of a pie is greater than the weight limit of a rack, variable f is incremented by 1. If at the end of the inner for loop, f is equal to d (the number of pies), g is incremented by 1. g is used for counting the number of pies that can not be cooled. The result is d - g (total pies minus the number of pies that can’t be cooled).

@gautam94. here is what you are doing in your code:- If the weight of a pie is greater than the all the weights that can be accomodated on the rack, then it cant be accomodated somewhere on the rack. now consider the case pies:- 10 10, racks :- 5 15. in this case. here 10 is not greater than 15 so you will count that both pies can be accomodated on the racks(in rack of weight 15) but in reality the rack of 15 cannot be used once 1 pie of 10 is placed there.output should be 1 but your code will return 2 as it assumes rack of 15 will be used twice. so try to come up with a logic to handle these cases. Also try to make your code more readable by avoiding use of pre-increment “++a” operator as you will find it difficult to track your code.

1 Like