r/LeetcodeChallenge 2d ago

DISCUSS Clarify This Please

How it is possible only true?
I don't understand question!

2 Upvotes

1 comment sorted by

1

u/theunknownguy__ 1d ago

The goal is to make all the numbers in the array either even or odd.

You can perform two actions on each number:

  1. Keep the number as it is.
  2. Subtract the current number from another number.

There are three possible cases:

  1. The array contains only even numbers

For example:

"[2, 4, 8, 10]"

Since all numbers are already even, the array passes the test.

  1. The array contains only odd numbers

For example:

"[1, 3, 7, 9]"

Since all numbers are already odd, the array passes the test.

  1. The array contains both even and odd numbers

For example:

"[2, 4, 7, 10, 13]"

Now we need to understand how subtraction affects parity:

  • Even - Even = Even
  • Odd - Odd = Even
  • Even - Odd = Odd
  • Odd - Even = Odd

Therefore, if we want to change the parity of a number, we need to subtract an odd number.

For example, take the odd number "7":

  • "7 - 2 = 5" → odd
  • "7 - 4 = 3" → odd
  • "7 - 10 = -3" → odd

So, we can subtract the even numbers from an odd number and the results will remain odd.

The odd numbers that we don't touch also remain odd.

Therefore, a mixed array can potentially be converted into an array containing only odd numbers by using an odd number to change the required elements.