[Solved] Shyam has N Jars of Ladoos and he wants to distribute the Ladoos amongst M Villagers with Python

Shyam has N Jars of Ladoos and he wants to distribute the Ladoos amongst M Villagers. The i-th jar contains Li pieces of Ladoos. He wants to make sure that every villager gets the same amount of ladoos and that the number of ladoos they receive is the greatest possible. He can open each jar and mix all the ladoos before distributing them to the villagers. How many pieces of ladoos will remain after he shares them amongst villagers, based on the rules described above?

Input:
The first line of input contains two integers: integer N, the number of ladoos, and M, number of villagers.
The next line contains N non-negative integers.

Output:
The remaining number of ladoos according to rule described above.

Input:

7 3
1 2 3 4 5 6 7

Output:

1

Explanation:
we have N=7 Jars of Ladoos. In total, we have 1+2+3+4+5+6+7=28 ladoos that we want to divide between M=3 villagers. Every villager can get 9 pieces of ladoos, so 28-3×9=1 pieces of ladoos will remain.

Sample Test Cases

Test Case 1

7 3
1 2 3 4 5 6 7

1

Test Case 2

5 10

7 7 7 7 7

5

Test Case 3

8 17

8 10 6 11 15 8 16 4

10

Test Case 4

5 11

13 17 2 8 19

4

Test Case 5

6 17

14 5 8 17 14 6

13

Solution

Steps:

  1. Take input of n and k
  2. Take input of array
  3. Sum the array
  4. Print the sum%k
n, k = map(int, input().split())
arr = list(map(int, input().split()))
s = sum(arr)

print(s%k)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *