This is Counting Problem!!!
This was one of those problems that just tests your patience to the limits and obliges you to fully exhaust your mind, and not to mention I had a math exam today, and yesterday 6 hours of sleep. Yes, I started thinking about this one from like 1 hour ago, that is 10:35p.m. something, and it's 11:35 right now. You can read the problem statement and try it on your own, but I would just say that negative and negative are not always positive...well, it wasn't the first time I had attempted this problem, this one is an old enemy, last time, I couldn't figure this out and from then it was in my tabs, now I am taking the problems for which I wasn't able to come up with an idea.
The good part was that after the torture of 1 hour thinking about all the corner cases and all the possibilities that were possible, my code passed all the test cases in the first go. And I think that this is an important point that, many times we just come up with an idea not thinking about it much and we just run it hoping that it will pass, but the thing is that the more you think about the problem the better it is for you although it is very difficult to think about these tricky problems with full concentration but that's what is required, it took me 1 hour to understand the core of this problem, but someone with more focus, concentration, or experience could have done it in less time, also I was feeling difficulty with focusing because I generally can't function if I get less than 8 hours of sleep.
I think now I am gonna hit the bed before my brain starts throwing fatal errors on me, it has already processed a lot.....well, the
Problem
You are given an array A=[A1,A2,…,AN].
Is it possible to partition A into two non-empty subsequences S1 and S2 such that sum(S1)×sum(S2) is odd?
Here, sum(S1) denotes the sum of elements in S1, and sum(S2) is defined similarly.
Note: S1 and S2 must partition A, that is:
- S1 and S2 must be non-empty
- Every element of A must be in either S1 or S2
- S1 and S2 must be disjoint (in terms of which indices their subsequences represent)
Input Format
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of 2 lines of input.
- The first line of each test case contains a single integer N, the size of the array.
- The next line contains N space-separated integers A1,A2,…,AN: the elements of the array.
Output Format
For each test case, print on a new line the answer: YES
if the array can be partitioned into two subsequences satisfying the condition, and NO
otherwise.
Each character of the output may be printed in either uppercase or lowercase, i.e, YES
, yes
, YEs
, and yEs
will all be treated as equivalent.
Constraints
- 1≤T≤105
- 2≤N≤105
- 1≤Ai≤109
- The sum of N across all test cases won't exceed 106.
Sample 1:
Explanation:
Test case 1: We have A=[1,1,2,2]. Let S1 be the underlined elements and S2 be the other ones. sum(S1)×sum(S2)=3×3=9.
Test case 2: It can be proved that no partition of A into S1,S2 satisfies the condition.
Test case 4: Choose S1={3},S2={5}.
Test case 4: It can be proved that no partition of A into S1,S2 satisfies the condition.
This is the code for the above problem, try to think about the solution
of the problem on your own and after that try to understand this code
T=int(input())
while(T):
n=int(input())
nums=list(map(int,input().split()))
odd_count,even_count=0,0
for i in nums:
if(i%2==0):
even_count+=1
else:
odd_count+=1
# When there are odd numbers with count less than equal to 1
if(odd_count<=1):
print("NO")
T-=1
continue
# When there is no even number in the array
if(even_count==0):
if(odd_count%2!=0 or (odd_count/2)%2==0):
print("NO")
# elif((odd_count/2)%2!=0):
# print("YES")
else:
print("YES")
T-=1
continue
# Now till here we know that we have odd_count greater than equal to 2 and also
# we have 1 or more even numbers
if(odd_count%2==0):
print("YES")
else:
print("NO")
T-=1
Comments
Post a Comment