Sort Array By Parity
EasyArraySorting
Description
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.
Examples
Input:
nums = [3,1,2,4]Output:
[2,4,3,1]Explanation:
Using two-pointer approach: swap even 2 with odd 3, swap even 4 with odd 1. Even numbers (2,4) move to front, odds (3,1) to back.
Input:
nums = [1,3,5,7]Output:
[1,3,5,7]Explanation:
All numbers are odd, so they remain in their original positions since there are no even numbers to move to the beginning.
Input:
nums = [8,6,0,2,9,11]Output:
[8,6,0,2,9,11]Explanation:
The first four numbers (8,6,0,2) are all even and already at the beginning, followed by the odd numbers (9,11), so the array is already properly sorted by parity.
Constraints
- •
1 ≤ nums.length ≤ 5000 - •
0 ≤ nums[i] ≤ 5000
Ready to solve this problem?
Practice solo and sharpen your skills for technical interviews.