Partition an Array into Even Values Followed by Odd Values
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
### Problem
Rearrange every value of `nums` so all even integers appear before all odd integers. Preserve every occurrence. Any ordering within the even and odd portions is acceptable.
```cpp
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
Input: nums = [5,0,3,2]
Output: [0,2,5,3]
Explanation: The two evens precede both odds;[2,0,3,5] is another valid answer.
```
**Example 2:**
```text
Input: nums = [7,1,9]
Output: [7,1,9]
Explanation: With no even values, the original order already satisfies the requirement.
```
### Constraints
- `1 <= nums.length <= 5000`.
- `0 <= nums[i] <= 5000`.
### Contract considerations
The result need not be numerically sorted; it must only place every even value before every odd value.
```hint Read the boundary condition
Maintain the index where the next even value belongs.
```
### What a Strong Answer Covers
The result need not be numerically sorted; it must only place every even value before every odd value.
### Follow-up Questions
Optional practice extension, separate from the original task: How could you preserve the original relative order within each parity group?
Overview: Partition an Array into Even Values Followed by Odd Values. Rearrange every value of nums so all even integers appear before all odd integers.
Partition an Array into Even Values Followed by Odd Values
Amazon
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0
Problem
Rearrange every value of nums so all even integers appear before all odd integers. Preserve every occurrence. Any ordering within the even and odd portions is acceptable.
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums);
};
Examples
Example 1:
Input: nums = [5,0,3,2]
Output: [0,2,5,3]
Explanation: The two evens precede both odds;[2,0,3,5] is another valid answer.
Example 2:
Input: nums = [7,1,9]
Output: [7,1,9]
Explanation: With no even values, the original order already satisfies the requirement.
Constraints
1 <= nums.length <= 5000
.
0 <= nums[i] <= 5000
.
Contract considerations
The result need not be numerically sorted; it must only place every even value before every odd value.
What a Strong Answer Covers Guidance
The result need not be numerically sorted; it must only place every even value before every odd value.
Follow-up Questions Guidance
Optional practice extension, separate from the original task: How could you preserve the original relative order within each parity group?