Sort Array By Parity
EasyPrompt
Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.
Return any array that satisfies this condition.
Example 1:
Input: nums = [3,1,2,4]
Output: [2,4,3,1]
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.Example 2:
Input: nums = [0]
Output: [0]
Constraints:
1 <= nums.length <= 50000 <= nums[i] <= 5000
Approaches
3 approaches with complexity analysis and trade-offs.
This approach treats the problem as a custom sorting problem. We can use a standard sorting algorithm, but with a custom comparison logic. The logic is simple: even numbers are considered "smaller" than odd numbers. This ensures that after sorting, all even numbers will appear before all odd numbers.
Algorithm
- Create a new
Integerarray of the same size as the inputnumsarray. - Copy the elements from
numsto the newIntegerarray. - Use
Arrays.sort()with a custom lambda comparator:(a, b) -> Integer.compare(a % 2, b % 2). This sorts the array by placing even numbers (wherex % 2is 0) before odd numbers (wherex % 2is 1). - Copy the sorted elements from the
Integerarray back to the originalnumsarray. - Return the modified
numsarray.
Walkthrough
The core idea is to define a custom comparator that guides the sorting process. The comparator for two numbers, a and b, will return a negative value if a should come before b, a positive value if b should come before a, and zero if their order doesn't matter relative to the parity rule. The comparison can be based on the result of the modulo-2 operation (x % 2). An even number gives 0, and an odd number gives 1. So, we can simply sort based on a % 2 vs b % 2. In Java, Arrays.sort() on primitive arrays doesn't accept a custom comparator. A common way to handle this is to convert the int[] to an Integer[], sort the Integer[], and then copy it back.
import java.util.Arrays; class Solution { public int[] sortArrayByParity(int[] nums) { Integer[] numsInteger = new Integer[nums.length]; for (int i = 0; i < nums.length; i++) { numsInteger[i] = nums[i]; } Arrays.sort(numsInteger, (a, b) -> Integer.compare(a % 2, b % 2)); for (int i = 0; i < nums.length; i++) { nums[i] = numsInteger[i]; } return nums; }}Complexity
Time
O(N log N). The dominant operation is the sort, which typically has this time complexity. The conversions to and from `Integer[]` take O(N) time.
Space
O(N). We need an auxiliary array of `Integer` objects to use the custom comparator with `Arrays.sort`.
Trade-offs
Pros
Conceptually simple and relies on a well-known library function.
The code is concise and easy to write.
Cons
Not the most efficient time complexity for this specific problem.
Requires extra space for the boxed
Integerarray, which also adds overhead.
Solutions
Solution
class Solution {public int[] sortArrayByParity(int[] nums) { int i = 0, j = nums.length - 1; while (i < j) { if (nums[i] % 2 == 0) { ++i; } else if (nums[j] % 2 == 1) { --j; } else { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; ++i; --j; } } return nums; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.