🧠
Two Pointers (In-Place Partition)
💡 This approach improves space usage by rearranging elements in-place using two pointers, a common technique in partitioning problems.
Intuition
Use two pointers from the start and end of the array to swap misplaced even and odd numbers until pointers meet.
Algorithm
- Initialize two pointers: left at start, right at end.
- While left < right, move left forward if nums[left] is even.
- Move right backward if nums[right] is odd.
- If nums[left] is odd and nums[right] is even, swap them and move both pointers.
- Repeat until pointers meet or cross.
💡 This method partitions the array in one pass without extra space, but requires careful pointer movement to avoid errors.
from typing import List
def partitionArrayByParity(nums: List[int]) -> List[int]:
left, right = 0, len(nums) - 1
while left < right:
while left < right and nums[left] % 2 == 0:
left += 1
while left < right and nums[right] % 2 == 1:
right -= 1
if left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1
return nums
# Driver code
if __name__ == '__main__':
print(partitionArrayByParity([3,1,2,4])) # Output: [4,2,1,3] or any valid partition
Line Notes
left, right = 0, len(nums) - 1Initialize two pointers at array ends
while left < right:Loop until pointers meet or cross
while left < right and nums[left] % 2 == 0:Move left pointer forward if current is even
while left < right and nums[right] % 2 == 1:Move right pointer backward if current is odd
if left < right:Check if pointers still valid for swapping
nums[left], nums[right] = nums[right], nums[left]Swap misplaced odd and even numbers
left += 1Move left pointer after swap
right -= 1Move right pointer after swap
return numsReturn the partitioned array
import java.util.*;
public class PartitionArrayByParity {
public static int[] partitionArrayByParity(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
while (left < right && nums[left] % 2 == 0) left++;
while (left < right && nums[right] % 2 == 1) right--;
if (left < right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
right--;
}
}
return nums;
}
public static void main(String[] args) {
int[] input = {3,1,2,4};
int[] output = partitionArrayByParity(input);
System.out.println(Arrays.toString(output)); // Output: [4,2,1,3] or any valid partition
}
}
Line Notes
int left = 0, right = nums.length - 1;Initialize pointers at array boundaries
while (left < right)Loop until pointers meet or cross
while (left < right && nums[left] % 2 == 0) left++;Advance left pointer past evens
while (left < right && nums[right] % 2 == 1) right--;Move right pointer past odds
if (left < right)Check if swap is needed
int temp = nums[left];Store left value temporarily for swap
nums[left] = nums[right];Assign right value to left position
nums[right] = temp;Complete swap by assigning temp to right
left++; right--;Move pointers inward after swap
return nums;Return the rearranged array
#include <iostream>
#include <vector>
using namespace std;
vector<int> partitionArrayByParity(vector<int>& nums) {
int left = 0, right = (int)nums.size() - 1;
while (left < right) {
while (left < right && nums[left] % 2 == 0) left++;
while (left < right && nums[right] % 2 == 1) right--;
if (left < right) {
swap(nums[left], nums[right]);
left++;
right--;
}
}
return nums;
}
int main() {
vector<int> input = {3,1,2,4};
vector<int> output = partitionArrayByParity(input);
for (int num : output) cout << num << ' ';
cout << endl; // Output: 4 2 1 3 or any valid partition
return 0;
}
Line Notes
int left = 0, right = (int)nums.size() - 1;Initialize two pointers at array ends
while (left < right)Loop until pointers cross
while (left < right && nums[left] % 2 == 0) left++;Skip even numbers from left
while (left < right && nums[right] % 2 == 1) right--;Skip odd numbers from right
if (left < right)Check if swap needed
swap(nums[left], nums[right]);Swap misplaced odd and even numbers
left++; right--;Move pointers inward after swap
return nums;Return the partitioned array
function partitionArrayByParity(nums) {
let left = 0, right = nums.length - 1;
while (left < right) {
while (left < right && nums[left] % 2 === 0) left++;
while (left < right && nums[right] % 2 === 1) right--;
if (left < right) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
right--;
}
}
return nums;
}
// Test
console.log(partitionArrayByParity([3,1,2,4])); // Output: [4,2,1,3] or any valid partition
Line Notes
let left = 0, right = nums.length - 1;Initialize pointers at array boundaries
while (left < right)Loop until pointers meet or cross
while (left < right && nums[left] % 2 === 0) left++;Move left pointer past evens
while (left < right && nums[right] % 2 === 1) right--;Move right pointer past odds
if (left < right)Check if swap is needed
[nums[left], nums[right]] = [nums[right], nums[left]];Swap odd and even numbers
left++; right--;Move pointers inward after swap
return nums;Return the rearranged array
Each element is visited at most once by either pointer, so time is linear. No extra space is used beyond variables, so space is constant.
💡 For n=100,000, this means about 100,000 operations and no extra memory, making it efficient for large inputs.
Interview Verdict: Accepted and optimal
This is the preferred approach in interviews due to its in-place partitioning and linear time complexity.