Given an integer array arr containing both even and odd numbers, reorder the array so that all even numbers appear before all odd numbers. The relative order among evens or among odds does not need to be preserved unless otherwise specified.
Use two pointers left and right at the ends of the array. Move left forward until it finds an odd number and move right backward until it finds an even number. Swap those two elements. Repeat until left >= right.
This partitions evens to the left and odds to the right in-place, but it does not preserve the original order (unstable).
packagemaintypeSolutionstruct{}
// arrange reorders array with even numbers first (in-place)func (sSolution) arrange(arr []int) []int {
left, right:=0, len(arr)-1forleft < right {
forleft < right&&arr[left]%2==0 {
left++ }
forleft < right&&arr[right]%2!=0 {
right-- }
ifleft < right {
arr[left], arr[right] = arr[right], arr[left]
left++right-- }
}
returnarr}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
// non-static instance method `arrange`publicint[]arrange(int[] arr) {
int left = 0, right = arr.length- 1;
while (left < right) {
while (left < right && arr[left]% 2 == 0) left++;
while (left < right && arr[right]% 2 != 0) right--;
if (left < right) {
int tmp = arr[left]; arr[left]= arr[right]; arr[right]= tmp;
left++; right--;
}
}
return arr;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defarrange(self, arr: list[int]) -> list[int]:
left, right =0, len(arr) -1while left < right:
while left < right and arr[left] %2==0:
left +=1while left < right and arr[right] %2!=0:
right -=1if left < right:
arr[left], arr[right] = arr[right], arr[left]
left +=1 right -=1return arr
import java.util.*;
classSolution {
// stable arrange: returns a new array with evens followed by odds (order preserved)publicint[]arrange(int[] arr) {
List<Integer> evens =new ArrayList<>();
List<Integer> odds =new ArrayList<>();
for (int x : arr) {
if (x % 2 == 0) evens.add(x); else odds.add(x);
}
int[] res =newint[arr.length];
int i = 0;
for (int v : evens) res[i++]= v;
for (int v : odds) res[i++]= v;
return res;
}
}
1
2
3
4
5
6
classSolution:
# stable arrange: preserves relative order within evens and oddsdefarrange(self, arr: list[int]) -> list[int]:
evens = [x for x in arr if x %2==0]
odds = [x for x in arr if x %2!=0]
return evens + odds
Many standard libraries provide partition functions that move elements satisfying a predicate to the front. For example, std::partition (C++) or filter-based constructs can express the same idea concisely.
// Use std::partition to move evens before odds (in-place). This is linear time.
#include<algorithm>classSolution {
public: std::vector<int> arrange(std::vector<int> arr) {
std::partition(arr.begin(), arr.end(), [](int x){ return x %2==0; });
return arr;
}
};
# list comprehensionsclassSolution:
# library helper: sorted with key partitions by parity (stable), or use comprehensionsdefarrange(self, arr: list[int]) -> list[int]:
# Option A: stable partition via sorted (O(n log n))return sorted(arr, key=lambda x: x %2)
# Option B: list comprehensions (O(n)) — shown elsewhere in Method 2# evens = [x for x in arr if x % 2 == 0]# odds = [x for x in arr if x % 2 != 0]# return evens + odds