I tried solving the practice question on Google Kickstart and the solution I came up with seems to be wrong [I honestly don't know/understand why]. The question basically asks us to return the number of remaining candy after sharing n number of candy to m children. All children should have the same number of candy and each child must receive maximum number of candy possible.
Example ( thought process) : Given 5 bags of candy and 2 children, we have a total of 25 candy where each bag contains 5 candy (i.e. 5 5 5 5 5). Share all the candy in such a way that each child receives the maximum amount of candy and all children receive equal number of candy pieces. We start sharing the candy one child at a time in alternation. by the time both children receive 12 candy each, we'll have only 1 candy left. Since we cannot break one candy into half, we return 1 as the remainder.
The input is made in this format:
The first line of the input gives the number of test cases, T. T test cases follow.
Each test case consists of two lines. The first line of each test case contains two integers: integer N, the number of candy bags, and M, the number of kids.
The next line contains N non-negative integers C[0],C[1],…,C[N] representing array C, where the i-th integer represents the number of candies in the i-th bag.
Here is an example; there are 2 test cases:
2
7 3
1 2 3 4 5 6 7
5 10
7 7 7 7 7
In the first test case, there are 7 bags of candy and 3 children to share the candy amongst. Candy bag 1 has 1 candy, candy bag 2 has 2 candies, candy bag 3 has 3 candies, candy bag 4 has 4 candies, candy bag 5 has 5 candies, candy bag 6 has 6 candies, and candy bag 7 has 7 candies.
In the second test case, there are 5 candies and 10 children to share the candy amongst. All 5 candy bags have 7 candies in each.
The output is expected to be of this format:
Case #1: 1
Case #2: 5
Explanation / Thought process:
In the first test case, there's a grand total of 28 candies. We have to share all 28 equally between 3 children and return what's left as the answer. We can start sharing the candy and we find out that after all the kids have received 9 candies, we are left with 1 candy and we can't share/break/cut the candy into 3 equal (1/3)s so we return 1 as the answer to the first test case.
In the second test case, there is a grand total of 35 candies. We have to share all 35 candies equally between 10 children. After giving each child up to just 3 candies, we find out that we have only 5 candies left and we cannot share/break/cut up 5 candies to share amongst 10 children, hence we return 5 as the answer to the second test case.
This is my code (in Python 3)
num_testcases = int(input())
for etc in range(num_testcases):
n, m = map(int, input().split())
candies = input().split()
candies_arr = [int(i) for i in candies]
result = sum(candies_arr) % m
print('#Case {}: {}'.format(etc 1, result))
I would really like to learn/understand why my code isn't accepted as the correct answer. I probably must have underestimated the difficulty of the question and my thought process may be wrong. Thank you all for your time and I hope a favourable reply.
Best regards.
CodePudding user response:
Your output is incorrectly formatted.
This is what you're using:
print('#Case {}: {}'.format(etc 1, result))
What you actually need to use:
print('Case #{}: {}'.format(etc 1, result))