The code calculates a number of even numbers (evenCount
) and odd numbers (oddCount
) in the array.
Every two even numbers make a pair that gives an even number in sum. The number of such pairs is evenCount * (evenCount - 1) /2
. There is evenCount
ways to select the first even number, evenCount - 1
to select the second and apparently (a, b)
is the same pair as (b, a)
, so the division by two.
The same is with odd numbers. Every two odd numbers make a pair that gives an even number in sum.
This is how your get temp = (evenCount * (evenCount - 1) / 2) + (oddCount * (oddCount - 1) / 2)
.
solved Any one explain how this works? [closed]