Array and Strings – Strings – Problem #10
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
Write a C program to perform the following game. You have N number of buckets and each bucket contains some specific number of balls. Take input the value of ‘N’ from the keyboard and the number of balls in each of the N buckets and store it in an array ball[] of size N. Perform successive iterations whereby at each iteration, you have to find the number of bucket(s) with the least number of balls and also the least value (say the value be x), and remove ‘x’ balls from each bucket. You have to continue the game till no balls in any bucket are left. Display the number of bucket(s) with the least number of balls as well as the number of balls remaining in each bucket after you have extracted the least value, i.e x from each bucket. For displaying the number of balls remaining after each iteration, you do not need to resize the ball[] array or declare any other array, simply print the non-zero values within the array side by side after each iteration. You can use only one array to perform all the operations. Consider the example below. Example: (Please take the input and display the output as shown) Input: Enter Number of Buckets: 5 Enter Positive Number of Balls for Each 5 Buckets: 7 5 2 7 2 [this will be stored in the ball[] array] Output: (You would need to print the iteration number as well as shown) ——–Iteration-1——— Number of buckets with least number of balls = 2 Numbers of balls in the Remaining buckets = 5 3 5 [Reason: Buckets 3 and 5 have the least number of balls, i.e 2 balls which is the value of x. Now remove x=2 balls from each bucket which provides the balls remaining in the other buckets. Since bucket 3 and 5 have 0 balls remaining you do not print it] ——–Iteration-2——— Number of buckets with least number of balls = 1 Numbers of balls in the Remaining buckets = 2 2 [Reason: Continue from iteration 1. Bucket 2 has the least number of balls, i.e 3 balls which is the value of x. Now remove x=3 balls from each bucket which provides the balls remaining in the other buckets. Since bucket 2 have 0 balls remaining and buckets 3 and 5 already had 0 balls from the previous iteration, you do not print it] ——–Iteration-3——— Number of buckets with least number of balls = 2 Numbers of balls in the Remaining buckets = NIL [END]