Python Bookpages programme Codechef solution
Problem:
Chef has N books, such that the ith book has AI pages. He wants to divide all the books between Alice and Bob so that:
-
Both Alice and Bob get at least one book.
-
The number of pages allotted to Alice and Bob is either odd or even.
Find out whether such a distribution exists.
Input Format
-
The first line of input will contain a single integer T, denoting the number of test cases.
-
Each test case consists of multiple lines of input.
-
The first line of each test case contains a single integer N, the number of books.
-
The next line contains N space-separated integers, where the ith integer denotes the number of pages in the ith book.
Output Format
For each test case, output on a new line yes if a valid distribution exists. Otherwise, print no.
You can print each character in uppercase or lowercase. For example, NO, NO, NO, and nO are all considered the same.
Constraints:
-
1<=T<=100
-
2<=N<=10^5
-
1<=A<=100
-
The sum of N over all test cases won’t exceed 2.105.
Sample Input:
3
3
2 4 6
4
1 1 2 3
6
3 1 1 1 1 1
Sample Output:
YES
NO
YES
Explanation:
Test case 1: Consider the distribution where Alice gets books 1 and 3, and Bob gets book 2. Thus, the number of pages allotted to Alice is 2+ 6 = 8, and the number of pages allotted to Bob is 4.
Thus, both Alice and Bob have an even number of pages allotted to them.
Test case 2: It can be proven that there is no valid distribution.
Test case 3: Consider the distribution where Alice gets the books 1, 2, and 3, and Bob gets the books 4, 5, and 6. Thus, the number of pages allotted to Alice is 3+1+1=5, and the number of pages allotted to Bob is 1+1+1=3.
Thus, both Alice and Bob have an odd number of pages allotted to them.
Solution:
for i in range(int(input(“Enter the number of terms: “))):
N=int(input(“Enter the number of books: “))
b=list(map(int,input(“Enter number of pages separated by a space: “).split()))
c=sum(b)
if c%2==0:
print("YES")
else:
print("NO")
Steps to solve this problem:
-
Ask the user to enter the number of terms to execute the loop.
-
In loop, ask the user to enter the number of books.
-
Again, ask the user to enter the number of pages. Here we used the split() function to take multiple inputs, and we also used the map() function to get an iterator object, which is then passed as an argument to a list function.
-
After getting a list, which is b, we used the sum() function to calculate the sum of all elements present in the list and store it in c.
-
If the division of c with 2 yields 0 as the remainder, then print yes; otherwise, print no.