Sort Array By Parity II
The task is to sort an array in such a way that all the even integers are at even indices, and odd integers are at odd indices. Since we know that the array contains an equal number of even and odd numbers, we can achieve this using two pointers.
Approach
- Initialize Pointers: Create two pointers
even
andodd
. Seteven
at the first even index (0) andodd
at the first odd index (1). - Partition Elements: Loop until
even
andodd
are within the length of the array.- If the element at
even
index is even, incrementeven
by 2. - If the element at
odd
index is odd, incrementodd
by 2. - If both conditions fail, it means
nums[even]
is odd andnums[odd]
is even, so swap them, and increment botheven
andodd
by 2.
- If the element at
- Return Result: The array will now have even elements at even indices and odd elements at odd indices.
Code
|
|
Example
- Input:
nums = [4,2,5,7]
- Output:
[4,5,2,7]
(other valid outputs are also possible) - Explanation: Even numbers (4 and 2) are at even indices, and odd numbers (5 and 7) are at odd indices.
Insights
- Time Complexity: The time complexity is (O(n)), where (n) is the length of the array. The algorithm scans the array once.
- Space Complexity: The space complexity is (O(1)), as the sorting is done in place, and no additional space is used.
- In-Place Sorting: This solution sorts the original array in place, achieving a space-efficient solution.
title: Sort Array By Parity II tags: odd even linear-scan
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. You may return any answer array that satisfies this condition.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
Discussion
0 1 2 3 4 5 6 7
[4,4,4,4,7,7,7,7]
[4,4,4,7,4,7,7,7]
[4,7,4,7,4,7,4,7]
^
^
0 1 2 3
Input: [4,2,5,7]
Output: [4,5,2,7]
[2,7,4,5]
- Check if the number is odd, the index must also be odd and vice versa. 0 index is considered as even index.
- Sorting here does not really mean the elements are in ascending order.
- Iterate through the array.
- Have two pointers, one for odd index and for even index.
- Even pointer will put the elements in the even indices.
- Odd pointer will put the elements in the odd indices.
- One pass sorting algorithm.
- Start at index = 0 for even, for odd start from 0.
- Create an auxiliary array and put them into the correct index
- Invariant
- Odd value in odd index, even value must be even index
Implementation
|
|
Time and Space Complexity
Time: O(n)
Space: O(n)
Refactored Solution
|
|
0 1 2 3
Input: [4,2,5,7] Output: [4,5,2,7] [2,7,4,5]
Check if the number is odd, the index must also be odd and vice versa 0 index is considered as even index
Sorting here does not really mean the elements are in ascending order
Iterate through the array Have two pointers, one for odd index and for even index Even pointer will put the elements in the even indices Odd pointer will put the elements in the odd indices
One pass sorting algorithm Start at index = 0 for even, for odd start from 0,
Create an auxiliary array and put them into the correct index
Invariant
- Odd value in odd index, even value must be even index
|
|