Practice Minimum Adjacent Swaps to Alternate Parity
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
You are given an array nums of distinct integers. In one operation, you can swap any two adjacent elements in the array. An arrangement of the array is considered valid if the parity of adjacent elements alternates, meaning every pair of neighboring elements consists of one even and one odd number. Return the minimum number of adjacent swaps required to transform nums into any valid arrangement. If it is impossible to rearrange nums such that no two adjacent elements have the same parity, return -1.
Source: Minimum Adjacent Swaps to Alternate Parity on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [2,4,6,5,7]
3
# Example 2
nums = [2,4,5,7]
1
# Example 3
nums = [1,2,3]
0
# Example 4
nums = [4,5,6,8]
-1
Constraints
Related LeetCode Problems
Practice more problems similar to Minimum Adjacent Swaps to Alternate Parity.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.