BOOKPAGES - Editorial

Problem Link - Divide Pages

Problem Statement

Chef has N books such that the iii-th book has A[i] pages. He wants to divide all the books between Alice and Bob such that:

  • Both Alice and Bob get at least one book;
  • The number of pages allotted to Alice and Bob are either both odd or both even.

Find whether such distribution exists.

Approach

The code counts the number of books with an odd number of pages. If this count is odd, it means that one of Alice or Bob would have to receive an even number of pages while the other receives an odd number, which violates the condition. Conversely, if the count is even, it’s possible to distribute the books such that both receive either an odd or even total number of pages. The logic checks the parity of the odd count and prints NO for odd and YES for even.

Time Complexity

The time complexity is O(N) for each test case, where N is the number of books.

Space Complexity

The space complexity is O(N) due to the storage of the pages in the vector