#3587

Minimum Adjacent Swaps to Alternate Parity

specialist · 995 · lc medium +32 · 42.2% accepted · 92 likes · top 23%

Description

An array nums of distinct integers is given. One operation swaps any two adjacent elements.

A valid arrangement alternates parities — every pair of neighboring elements consists of one even and one odd number.

Return the minimum number of adjacent swaps to reach any valid arrangement.

Return -1 if such an arrangement is impossible.

Code

1
2
3