easyArraysPattern: Two Pointers
Partition Even Odd Solution
Problem Statement
You are given an array of integers `nums`. Reorder the array such that all elements at even indices come before all elements at odd indices.
Examples
Example 1:
Input:[3,1,2,4]
Output:[2,4,3,1]
Explanation: The even numbers (2,4) are placed before the odd numbers (3,1).
Example 2:
Input:[0,1,2]
Output:[0,2,1]
Explanation: The even numbers (0,2) are placed before the odd number (1).
Constraints
- 1 <= n <= 5000
- 0 <= arr[i] <= 5000
Time: O(N) Space: O(1)
Two pointers. Left pointer finds odds, right pointer finds evens. Swap them when found. Time O(N), Space O(1).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
