![]() |
VOOZH | about |
Given an array arr[] of integers, the task is to partition the array based on even and odd elements. The partition has to be stable, meaning the relative ordering of all even elements must remain the same before and after partitioning, and the same should hold true for all odd elements.
Note: For a binary array (containing only 0s and 1s), this partition process is equivalent to sorting the array.
Examples:
Input: arr[] = [1, 0, 1, 1, 0, 0]
Output: [0 ,0 ,0, 1, 1, 1]
Explanation: All even numbers came before the odd numbers.Input: arr[] = [1, 2, 3, 4, 5]
Output: [2, 4, 1, 3, 5]
Explanation: All even numbers [2, 4] came before the odd numbers [1, 3, 5], and the relative ordering was also same.Input: arr[] = [-5, -2, 0, 4, 7, 9]
Output: [-2, 0, 4, -5, 7, 9]
Explanation: All even numbers [-2, 0, 4] came before the odd numbers [-5, 7, 9], and the relative ordering was also same.
The idea is to to use Naive Partitioning Algorithm, We use a temporary array to store the rearranged elements. We first iterate over the original array and add all the even elements to the temporary array, maintaining their original order. Next, we add all the odd elements to the temporary array in the same manner.
This guarantees that even elements appear before odd elements while preserving the relative order of similar elements. Finally, we copy the elements from the temporary array back into the original array.
-2 0 4 -5 7 9
Time Complexity: O(n), for traversing the array.
Auxiliary Space: O(n), for temporary array.