← 返回 cisco 的题目列表Minimum Swaps for Even / Odd Partition
类型:qbank
Given an integer array, compute the minimum number of arbitrary swaps needed so every even number appears before every odd number.
Requirements
Input line 1 is inputSize, the size of the array.
Input line 2 contains N space-separated integers inputArr.
In one operation, elements at any two indices may be swapped.
Return the minimum number of swaps required so all even elements are at the beginning and all odd elements are at the end.
The relative order inside the even group or odd group does not matter.
Constraints shown: 2 <= inputSize <= 10^5, 1 <= inputArr[i] <= 10^9.
The array contains at least one even and one odd element.
Examples
Input:
4
6 3 4 5
Output:
1
Explanation: swap 3 and 4; valid partitions include [6,4,3,5] and [4,6,5,3].
Notes
Count misplaced odds in the even prefix or use two pointers from both ends; each swap fixes one misplaced odd and one misplaced even.