Tap here to switch tabs
Problem
Submissions

Problem: Minimum Adjacent Swaps to Alternate Parity

med
30 min
Try to solve the Minimum Adjacent Swaps to Alternate Parity problem.

Statement

Given an array nums of distinct integers, determine the minimum number of adjacent swaps required to rearrange the array such that the parity of the elements alternates throughout the array.

An array has alternating parity if no two adjacent elements have the same parity.

Return the minimum number of adjacent swaps needed to achieve this configuration, or return −1-1 if it is impossible.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • All elements in nums are distinct.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Adjacent Swaps to Alternate Parity

med
30 min
Try to solve the Minimum Adjacent Swaps to Alternate Parity problem.

Statement

Given an array nums of distinct integers, determine the minimum number of adjacent swaps required to rearrange the array such that the parity of the elements alternates throughout the array.

An array has alternating parity if no two adjacent elements have the same parity.

Return the minimum number of adjacent swaps needed to achieve this configuration, or return −1-1 if it is impossible.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • All elements in nums are distinct.