941. Sort Array By Parity
EasySolutions in Python, Java, C++, TypeScript
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
Method Explanation: In-Place Two-Pointer Approach
The problem asks us to rearrange an array so that all even numbers come before all odd numbers. The relative order of elements within the even and odd groups does not need to be preserved. This suggests that an in-place partitioning algorithm would be very efficient.
Solution
Sort Array By Parity in Python
class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
# insert_pos marks the boundary for even numbers.
# All elements before insert_pos are even.
insert_pos = 0
# Iterate through the array with a 'current' pointer.
for i in range(len(nums)):
# If the current element is even.
if nums[i] % 2 == 0:
# Swap it with the element at insert_pos.
# This moves the even number to the front section.
nums[insert_pos], nums[i] = nums[i], nums[insert_pos]
# Move the boundary for the next even number.
insert_pos += 1
return nums
Sort Array By Parity in Java
class Solution {
public int[] sortArrayByParity(int[] nums) {
// insertPos marks the boundary for even numbers.
// All elements before insertPos are even.
int insertPos = 0;
// Iterate through the array with a 'current' pointer.
for (int i = 0; i < nums.length; i++) {
// If the current element is even.
if (nums[i] % 2 == 0) {
// Swap the even element with the element at insertPos.
// This moves the even number to the front section.
int temp = nums[insertPos];
nums[insertPos] = nums[i];
nums[i] = temp;
// Move the boundary for the next even number.
insertPos++;
}
}
return nums;
}
}
Sort Array By Parity in C++
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums) {
// insertPos marks the boundary for even numbers.
// All elements before insertPos are even.
int insertPos = 0;
// Iterate through the array with a 'current' pointer.
for (int i = 0; i < nums.size(); ++i) {
// If the current element is even.
if (nums[i] % 2 == 0) {
// Swap it with the element at insertPos.
// This moves the even number to the front section.
swap(nums[insertPos], nums[i]);
// Move the boundary for the next even number.
insertPos++;
}
}
return nums;
}
};
Sort Array By Parity in TypeScript
function sortArrayByParity(nums: number[]): number[] {
// insertPos marks the boundary for even numbers.
// All elements before insertPos are even.
let insertPos = 0;
// Iterate through the array with a 'current' pointer.
for (let i = 0; i < nums.length; i++) {
// If the current element is even.
if (nums[i] % 2 === 0) {
// Swap it with the element at the insert position.
// This moves the even number to the front section.
[nums[insertPos], nums[i]] = [nums[i], nums[insertPos]];
// Move the boundary for the next even number.
insertPos++;
}
}
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.