Sort Array By Parity
Move the even numbers of an array to the front and the odd numbers to the back, in place.
Rearrange an array of integers nums
Example 1
Output: [4, 2, 1, 3]
Explanation: [2, 4, 3, 1], [4, 2, 3, 1] and [2, 4, 1, 3] are also accepted.
Example 2
Output: [0]
The Idea
Whether a number is even or odd is called its parity, so "sort by parity" means putting the numbers into two groups: evens on the left, odds on the right. Nothing else about their order counts. That is why Example 1 accepts four different answers.
The easy way is to take a second, empty array, copy the even numbers into it first and then the odd ones. That works, but it needs a whole second array as big as the first. Can the numbers be moved into their groups inside nums
Look at [3, 1, 2, 4]
Swapping a wrong-side pair from the two ends is the whole idea. Put one marker, lohi
In the row labelled "start", lohilohi
In the last row, lohilohi[4, 2, 1, 3]
Walking two markers from opposite ends toward each other like this is called the two-pointer technique. Here each marker guards a zone: lohi
Solution Approach
Turning that idea into code means settling three things: what each marker does at each step, when to stop, and how to test whether a number is odd.
Moving the markers
What if the number at lolohihi
Only when both are wrong, an odd number at lohi
When to stop
When can the walk stop? The numbers left of lohilohilo < hi
Testing for odd numbers
How do we tell even from odd? The remainder after dividing by 2, written n % 2-3 % 2n % 2 != 0
Java Solution
The method sortArrayByParity
- First, put the marker
on the first position, 0, and the markerlo on the last position,hi .nums.length - 1 - Walk the markers toward each other with a
loop that runs as long aswhile .lo < hi - Inside the loop, if
is even, movenums[lo] right. Otherwise, iflo is odd, movenums[hi] left.hi - If neither holds,
is odd andnums[lo] is even. Swap them through a temporary variable,nums[hi] , and move both markers inward.tmp - When the loop ends, return
, the same array that came in, now rearranged.nums
The main
import java.util.Arrays;
public class SortArrayByParity {
public static int[] sortArrayByParity(int[] nums) {
int lo = 0; // everything left of lo is even
int hi = nums.length - 1; // everything right of hi is odd
while (lo < hi) {
if (nums[lo] % 2 == 0) {
lo++; // even on the left: already in place
} else if (nums[hi] % 2 != 0) {
hi--; // odd on the right: already in place
} else {
int tmp = nums[lo]; // odd on the left, even on the right: swap
nums[lo] = nums[hi];
nums[hi] = tmp;
lo++;
hi--;
}
}
return nums;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(sortArrayByParity(new int[] {3, 1, 2, 4})));
System.out.println(Arrays.toString(sortArrayByParity(new int[] {0})));
System.out.println(Arrays.toString(sortArrayByParity(new int[] {-3, 2, -5})));
System.out.println(Arrays.toString(sortArrayByParity(new int[] {5, 7, 9})));
System.out.println(Arrays.toString(sortArrayByParity(new int[] {1, 3, 6, 8, 5, 2})));
System.out.println(Arrays.toString(sortArrayByParity(new int[] {})));
}
}
Running it prints:
[4, 2, 1, 3]
[0]
[2, -3, -5]
[5, 7, 9]
[2, 8, 6, 3, 5, 1]
[]
[4, 2, 1, 3][0]lohi
Look at the third line, [2, -3, -5]% 2 != 0nums[hi] % 2 == 1[-5, 2, -3]
[5, 7, 9]hilo[2, 8, 6, 3, 5, 1]hi[]
Complexity
The work is a single walk of lohi
- Time: O(n) for an array of n numbers, because every pass of the loop moves
right,lo left, or both, and the two markers meet after at most n - 1 passes.hi - Space: O(1), because the numbers are swapped inside
with one temporary variable, and the result is the input array itself, not a copy.nums
With three groups instead of two, the same idea becomes the