Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [3,1,2,4] Output: [2,4,3,1] Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Example 2:
Input: nums = [0] Output: [0]
Constraints:
1 <= nums.length <= 50000 <= nums[i] <= 5000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[3,1,2,4]
[0]
sort-array-by-parity-ii)sort-even-and-odd-indices-independently)largest-number-after-digit-swaps-by-parity)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #905: Sort Array By Parity
class Solution {
public int[] sortArrayByParity(int[] nums) {
int i = 0, j = nums.length - 1;
while (i < j) {
if (nums[i] % 2 == 0) {
++i;
} else if (nums[j] % 2 == 1) {
--j;
} else {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
++i;
--j;
}
}
return nums;
}
}
// Accepted solution for LeetCode #905: Sort Array By Parity
func sortArrayByParity(nums []int) []int {
for i, j := 0, len(nums)-1; i < j; {
if nums[i]%2 == 0 {
i++
} else if nums[j]%2 == 1 {
j--
} else {
nums[i], nums[j] = nums[j], nums[i]
}
}
return nums
}
# Accepted solution for LeetCode #905: Sort Array By Parity
class Solution:
def sortArrayByParity(self, nums: List[int]) -> List[int]:
i, j = 0, len(nums) - 1
while i < j:
if nums[i] % 2 == 0:
i += 1
elif nums[j] % 2 == 1:
j -= 1
else:
nums[i], nums[j] = nums[j], nums[i]
i, j = i + 1, j - 1
return nums
// Accepted solution for LeetCode #905: Sort Array By Parity
impl Solution {
pub fn sort_array_by_parity(mut nums: Vec<i32>) -> Vec<i32> {
let (mut i, mut j) = (0, nums.len() - 1);
while i < j {
if nums[i] % 2 == 0 {
i += 1;
} else if nums[j] % 2 == 1 {
j -= 1;
} else {
nums.swap(i, j);
i += 1;
j -= 1;
}
}
nums
}
}
// Accepted solution for LeetCode #905: Sort Array By Parity
function sortArrayByParity(nums: number[]): number[] {
for (let i = 0, j = nums.length - 1; i < j; ) {
if (nums[i] % 2 === 0) {
++i;
} else if (nums[j] % 2 === 1) {
--j;
} else {
[nums[i], nums[j]] = [nums[j], nums[i]];
++i;
--j;
}
}
return nums;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.