958. Sort Array By Parity II
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to sort an array of integers nums in a specific way. The array has two key properties: its length is even, and exactly half of its elements are even, while the other half are odd. The goal is to rearrange the array such that for any index i, if i is even, nums[i] must also be even, and if i is odd, nums[i] must be odd. The problem also includes a follow-up asking if this can be solved in-place, which suggests an O(1) space complexity solution is preferred.
Solution
Sort Array By Parity II in Python
class Solution:
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
n = len(nums)
# j is the pointer for odd indices, starting from 1.
j = 1
# i is the pointer for even indices, iterating from 0, 2, 4, ...
for i in range(0, n, 2):
# If we find an odd number at an even index i.
# A number is odd if num % 2 == 1.
if nums[i] % 2 == 1:
# We need to find an even number at an odd index j to swap.
# We search for an even number at odd indices using pointer j.
while nums[j] % 2 == 1:
j += 2
# Now, nums[j] is an even number at an odd index.
# Swap the misplaced odd number at i with the misplaced even number at j.
nums[i], nums[j] = nums[j], nums[i]
return nums
Sort Array By Parity II in Java
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int n = nums.length;
// j is the pointer for odd indices, starting from 1.
int j = 1;
// i is the pointer for even indices, iterating from 0, 2, 4, ...
for (int i = 0; i < n; i += 2) {
// If we find an odd number at an even index i.
// A number is odd if num % 2 == 1.
if (nums[i] % 2 == 1) {
// We need to find an even number at an odd index j to swap.
// We search for an even number at odd indices using pointer j.
while (nums[j] % 2 == 1) {
j += 2;
}
// Now, nums[j] is an even number at an odd index.
// Swap the misplaced odd number at i with the misplaced even number at j.
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
return nums;
}
}
Sort Array By Parity II in C++
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
int n = nums.size();
// j is the pointer for odd indices, starting from 1.
int j = 1;
// i is the pointer for even indices, iterating from 0, 2, 4, ...
for (int i = 0; i < n; i += 2) {
// If we find an odd number at an even index i.
// A number is odd if num % 2 == 1.
if (nums[i] % 2 == 1) {
// We need to find an even number at an odd index j to swap.
// We search for an even number at odd indices using pointer j.
while (nums[j] % 2 == 1) {
j += 2;
}
// Now, nums[j] is an even number at an odd index.
// Swap the misplaced odd number at i with the misplaced even number at j.
swap(nums[i], nums[j]);
}
}
return nums;
}
};
Sort Array By Parity II in TypeScript
function sortArrayByParityII(nums: number[]): number[] {
const n = nums.length;
// j is the pointer for odd indices, starting from 1.
let j = 1;
// i is the pointer for even indices, iterating from 0, 2, 4, ...
for (let i = 0; i < n; i += 2) {
// If we find an odd number at an even index i.
// A number is odd if num % 2 === 1.
if (nums[i] % 2 === 1) {
// We need to find an even number at an odd index j to swap.
// We search for an even number at odd indices using pointer j.
while (nums[j] % 2 === 1) {
j += 2;
}
// Now, nums[j] is an even number at an odd index.
// Swap the misplaced odd number at i with the misplaced even number at j.
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}
return nums;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.