Help Understanding the Logic Behind z + ((on % 2) * 2) in 2149A Solution

Revision en1, by ahnaf09, 2025-10-04 10:04:11

Hello Everyone, Hope you all are doing well!

Problem: 2149A - Be Positive I was trying to solve this problem but got completely stuck. Then I looked at other people's submissions.

Submission: 340371416 I found this solution, and according to my understanding, it runs in 31ms. Most other solutions follow a similar idea, but I’m a bit confused about one part:

cout << z + ((on % 2) * 2) << endl;

I understand that z counts the number of zeros in the array and on counts the number of negative numbers. What I don’t understand is how this formula calculates the minimum number of positive 1s needed to make the product of all array elements positive.

Could someone please explain the logic behind z + ((on % 2) * 2) in simple words? It would help me a lot.

Thanks a lot for your time and help! Have a great day!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English ahnaf09 2025-10-04 10:04:11 954 Initial revision (published)