Sort Array By Parity - Complete Solution Guide
Sort Array By Parity is LeetCode problem 905, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 <= 5000 0 <= nums[i] <= 5000
Detailed Explanation
The problem asks us to rearrange the elements of an integer array `nums` such that all even integers precede all odd integers. The order among the even numbers or among the odd numbers is not specified, so multiple valid outputs are possible. The input is the integer array `nums`. The output is an integer array with even numbers at the beginning, followed by odd numbers.
Solution Approach
The Python solution utilizes a simple approach of creating two separate lists, one for even numbers and one for odd numbers. It iterates through the input array, appending each number to the appropriate list based on its parity (even or odd). Finally, it concatenates the even list with the odd list and returns the result. The Java and C++ solutions use a two-pointer approach for an in-place modification. The C solution uses a similar approach to the python one allocating a new array and sorting into the array. Both are valid.
Step-by-Step Algorithm
- Step 1: Python: Initialize two empty lists, `even` and `odd`. Java/C++: Initialize two pointers, `left` at the beginning of the array and `right` at the end of the array. C: Allocate new array of the same size.
- Step 2: Python: Iterate through the input array `nums`. Java/C++: Start a `while` loop that continues as long as `left < right`. C: Iterate through the input array `nums`.
- Step 3: Python: For each number, check if it's even (divisible by 2). If it is, append it to the `even` list; otherwise, append it to the `odd` list. Java/C++: If `nums[left]` is odd and `nums[right]` is even, swap them and move both pointers. If `nums[left]` is even, move `left` forward. If `nums[right]` is odd, move `right` backward. C: If the number is even add to the even index and increment it; otherwise add the number to the odd index and decrement it.
- Step 4: Python: After iterating through all numbers, concatenate the `even` list with the `odd` list. Java/C++: The array `nums` is now sorted by parity. C: The array is now sorted.
- Step 5: Python: Return the concatenated list. Java/C++: Return the modified array `nums`. C: return the new array.
Key Insights
- Insight 1: We need to separate even and odd numbers within the array.
- Insight 2: A two-pointer approach can be used for an in-place solution, potentially optimizing space complexity.
- Insight 3: The problem emphasizes that *any* valid output is acceptable, which relaxes the requirements and allows for simpler solutions.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Two Pointers, Sorting.
Companies
Asked at: DXC Technology.