Instagram
youtube
Facebook
Twitter

Python Sums divisible by 3 program in the Codechef solution

Python Sums divisible by 3 program in the Codechef solution

Problem:

Let us consider a multiset A consisting of 0s, 1s, and 2s. Define S(A) to be the sum of the elements in A.

It is guaranteed that S (A) is divisible by 3. You need to partition A into some number of non-empty multisets (A1, A2,..., Ak).

Input:

The first line contains one integer number, t, which is the number of test cases. The next two lines describe test cases.

The only line of each test case contains the three integers s0, s1, and s2, the numbers of 0s, 1s, and 2s in A, respectively.

Output:

For each test case, print the maximum number of nonempty multisets with a sum divisible by 3 into which the multiset A can be partitioned.

Constraints:

  • 1<=t<=10^5
  • 0<=s0,s1,s2<=10^9

Sample Input

4 239 0 0 2 4 1 0 0 3 7 3 3

Sample Output:

239 4 1 10

Solution:

t=int(input(“Enter the number of terms: “))

for i in range(t):

    a,b,c=map(int,input(“Enter 3 numbers: “).split())

    result=a+min(b,c)+abs(b-c)//3

    print(result)

 

           Steps to solve this problem:

  1. Ask the user to enter the number of terms and store it in
  2. In the loop, ask the user to enter 3 numbers, and using the map() function, convert them to iterator objects and store them in a, b, and c, respectively.
  3. Calculate the result by adding a with a minimum of b and c and with an absolute value of (b-c) floor divided by 3.
  4. Print the result.