|
1 | 1 | ---
|
2 |
| -id: sort-array-by-parity |
| 2 | +id: Sort-Array-By-Parity |
3 | 3 | title: Sort Array By Parity
|
4 |
| -sidebar_label: 0905 - Sort Array By Parity |
5 |
| -tags: |
6 |
| - - easy |
7 |
| - - Arrays |
8 |
| - - Algorithms |
| 4 | +sidebar_label: Sort Array By Parity |
| 5 | +tags: |
| 6 | + - Arrays |
| 7 | + - Sorting |
9 | 8 | ---
|
10 | 9 |
|
11 | 10 | ## Problem Description
|
12 | 11 |
|
13 |
| -Given an integer array `nums`, move all the even integers to the beginning of the array followed by all the odd integers. |
| 12 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 13 | +| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ | |
| 14 | +| [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/description/) | [Sort Array By Parity Solution on LeetCode](https://leetcode.com/problems/sort-array-by-parity/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) | |
14 | 15 |
|
15 |
| -Return any array that satisfies this condition. |
| 16 | +## Problem Description |
16 | 17 |
|
17 |
| -## Examples |
| 18 | +Given an integer array `nums`, move all the even integers at the beginning of the array followed by all the odd integers. |
18 | 19 |
|
19 |
| -**Example 1:** |
| 20 | +Return any array that satisfies this condition. |
20 | 21 |
|
21 |
| -``` |
22 |
| -Input: nums = [3,1,2,4] |
23 |
| -Output: [2,4,3,1] |
24 |
| -Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. |
25 |
| -``` |
| 22 | +### Example 1: |
26 | 23 |
|
27 |
| -**Example 2:** |
| 24 | +**Input:** `nums = [3, 1, 2, 4]` |
| 25 | +**Output:** `[2, 4, 3, 1]` |
| 26 | +**Explanation:** The outputs `[4, 2, 3, 1]`, `[2, 4, 1, 3]`, and `[4, 2, 1, 3]` would also be accepted. |
28 | 27 |
|
29 |
| -``` |
30 |
| -Input: nums = [0] |
31 |
| -Output: [0] |
32 |
| -``` |
| 28 | +### Example 2: |
| 29 | + |
| 30 | +**Input:** `nums = [0]` |
| 31 | +**Output:** `[0]` |
33 | 32 |
|
34 | 33 | ## Constraints
|
35 | 34 |
|
36 |
| -``` |
37 |
| -1 <= nums.length <= 5000 |
38 |
| -0 <= nums[i] <= 5000 |
39 |
| -``` |
| 35 | +- `1 <= nums.length <= 5000` |
| 36 | +- `0 <= nums[i] <= 5000` |
| 37 | + |
| 38 | +## Approach |
| 39 | + |
| 40 | +1. **Identify even and odd integers**: Iterate through the array and separate the even and odd integers. |
| 41 | +2. **Rearrange the array**: Place all even integers at the beginning of the array followed by the odd integers. |
40 | 42 |
|
41 | 43 | ## Solution
|
42 | 44 |
|
43 | 45 | ### Python
|
44 | 46 |
|
45 | 47 | ```python
|
46 |
| -class Solution: |
47 |
| - def sortArrayByParity(self, nums): |
48 |
| - return [x for x in nums if x % 2 == 0] + [x for x in nums if x % 2 != 0] |
49 |
| - |
50 |
| -# Example usage: |
51 |
| -solution = Solution() |
52 |
| -print(solution.sortArrayByParity([3,1,2,4])) # Output: [2,4,3,1] |
| 48 | +def sortArrayByParity(nums): |
| 49 | + even = [x for x in nums if x % 2 == 0] |
| 50 | + odd = [x for x in nums if x % 2 != 0] |
| 51 | + return even + odd |
| 52 | + |
| 53 | +# Example usage |
| 54 | +nums = [3, 1, 2, 4] |
| 55 | +print(sortArrayByParity(nums)) # Output: [2, 4, 3, 1] |
53 | 56 | ```
|
54 | 57 |
|
55 |
| -### C++ |
| 58 | +### Java |
56 | 59 |
|
57 |
| -```cpp |
58 |
| -#include <vector> |
59 |
| -#include <algorithm> |
| 60 | +```java |
| 61 | +import java.util.*; |
60 | 62 |
|
61 |
| -class Solution { |
62 |
| -public: |
63 |
| - std::vector<int> sortArrayByParity(std::vector<int>& nums) { |
64 |
| - std::vector<int> result; |
| 63 | +public class EvenOddArray { |
| 64 | + public static int[] sortArrayByParity(int[] nums) { |
| 65 | + List<Integer> even = new ArrayList<>(); |
| 66 | + List<Integer> odd = new ArrayList<>(); |
| 67 | + |
65 | 68 | for (int num : nums) {
|
66 | 69 | if (num % 2 == 0) {
|
67 |
| - result.push_back(num); |
| 70 | + even.add(num); |
| 71 | + } else { |
| 72 | + odd.add(num); |
68 | 73 | }
|
69 | 74 | }
|
70 |
| - for (int num : nums) { |
71 |
| - if (num % 2 != 0) { |
72 |
| - result.push_back(num); |
73 |
| - } |
| 75 | + |
| 76 | + even.addAll(odd); |
| 77 | + return even.stream().mapToInt(i -> i).toArray(); |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String[] args) { |
| 81 | + int[] nums = {3, 1, 2, 4}; |
| 82 | + System.out.println(Arrays.toString(sortArrayByParity(nums))); // Output: [2, 4, 3, 1] |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### C++ |
| 88 | + |
| 89 | +```cpp |
| 90 | +#include <vector> |
| 91 | +#include <iostream> |
| 92 | + |
| 93 | +std::vector<int> sortArrayByParity(std::vector<int>& nums) { |
| 94 | + std::vector<int> even, odd; |
| 95 | + for (int num : nums) { |
| 96 | + if (num % 2 == 0) { |
| 97 | + even.push_back(num); |
| 98 | + } else { |
| 99 | + odd.push_back(num); |
74 | 100 | }
|
75 |
| - return result; |
76 | 101 | }
|
77 |
| -}; |
| 102 | + even.insert(even.end(), odd.begin(), odd.end()); |
| 103 | + return even; |
| 104 | +} |
78 | 105 |
|
79 |
| -// Example usage: |
80 | 106 | int main() {
|
81 |
| - Solution solution; |
82 | 107 | std::vector<int> nums = {3, 1, 2, 4};
|
83 |
| - std::vector<int> result = solution.sortArrayByParity(nums); // Output: [2, 4, 3, 1] |
84 |
| - return 0; |
| 108 | + std::vector<int> result = sortArrayByParity(nums); |
| 109 | + for (int num : result) { |
| 110 | + std::cout << num << " "; |
| 111 | + } |
| 112 | + // Output: 2 4 3 1 |
85 | 113 | }
|
86 | 114 | ```
|
87 | 115 |
|
88 |
| -### Java |
89 |
| -
|
90 |
| -```java |
91 |
| -import java.util.ArrayList; |
92 |
| -import java.util.List; |
93 |
| -
|
94 |
| -class Solution { |
95 |
| - public int[] sortArrayByParity(int[] nums) { |
96 |
| - List<Integer> result = new ArrayList<>(); |
97 |
| - for (int num : nums) { |
98 |
| - if (num % 2 == 0) { |
99 |
| - result.add(num); |
100 |
| - } |
101 |
| - } |
102 |
| - for (int num : nums) { |
103 |
| - if (num % 2 != 0) { |
104 |
| - result.add(num); |
105 |
| - } |
| 116 | +### C |
| 117 | +
|
| 118 | +```c |
| 119 | +#include <stdio.h> |
| 120 | +#include <stdlib.h> |
| 121 | +
|
| 122 | +void sortArrayByParity(int* nums, int numsSize, int* returnSize) { |
| 123 | + int* result = (int*)malloc(numsSize * sizeof(int)); |
| 124 | + int evenIndex = 0, oddIndex = numsSize - 1; |
| 125 | + |
| 126 | + for (int i = 0; i < numsSize; ++i) { |
| 127 | + if (nums[i] % 2 == 0) { |
| 128 | + result[evenIndex++] = nums[i]; |
| 129 | + } else { |
| 130 | + result[oddIndex--] = nums[i]; |
106 | 131 | }
|
107 |
| - return result.stream().mapToInt(i -> i).toArray(); |
108 | 132 | }
|
| 133 | + *returnSize = numsSize; |
| 134 | + for (int i = 0; i < numsSize; ++i) { |
| 135 | + nums[i] = result[i]; |
| 136 | + } |
| 137 | + free(result); |
| 138 | +} |
109 | 139 |
|
110 |
| - public static void main(String[] args) { |
111 |
| - Solution solution = new Solution(); |
112 |
| - int[] nums = {3, 1, 2, 4}; |
113 |
| - int[] result = solution.sortArrayByParity(nums); // Output: [2, 4, 3, 1] |
114 |
| - for (int num : result) { |
115 |
| - System.out.print(num + " "); |
116 |
| - } |
| 140 | +int main() { |
| 141 | + int nums[] = {3, 1, 2, 4}; |
| 142 | + int numsSize = sizeof(nums) / sizeof(nums[0]); |
| 143 | + int returnSize; |
| 144 | + sortArrayByParity(nums, numsSize, &returnSize); |
| 145 | + |
| 146 | + for (int i = 0; i < numsSize; ++i) { |
| 147 | + printf("%d ", nums[i]); |
117 | 148 | }
|
| 149 | + // Output: 2 4 3 1 |
| 150 | + return 0; |
118 | 151 | }
|
119 | 152 | ```
|
120 | 153 |
|
121 | 154 | ### JavaScript
|
122 | 155 |
|
123 | 156 | ```javascript
|
124 |
| -var sortArrayByParity = function (nums) { |
125 |
| - let result = []; |
126 |
| - for (let num of nums) { |
127 |
| - if (num % 2 === 0) { |
128 |
| - result.push(num); |
129 |
| - } |
130 |
| - } |
131 |
| - for (let num of nums) { |
132 |
| - if (num % 2 !== 0) { |
133 |
| - result.push(num); |
| 157 | +function sortArrayByParity(nums) { |
| 158 | + let even = []; |
| 159 | + let odd = []; |
| 160 | + |
| 161 | + for (let num of nums) { |
| 162 | + if (num % 2 === 0) { |
| 163 | + even.push(num); |
| 164 | + } else { |
| 165 | + odd.push(num); |
| 166 | + } |
134 | 167 | }
|
135 |
| - } |
136 |
| - return result; |
137 |
| -}; |
| 168 | + |
| 169 | + return [...even, ...odd]; |
| 170 | +} |
138 | 171 |
|
139 |
| -// Example usage: |
140 |
| -console.log(sortArrayByParity([3, 1, 2, 4])); // Output: [2, 4, 3, 1] |
| 172 | +// Example usage |
| 173 | +let nums = [3, 1, 2, 4]; |
| 174 | +console.log(sortArrayByParity(nums)); // Output: [2, 4, 3, 1] |
141 | 175 | ```
|
| 176 | + |
| 177 | +## Step-by-Step Algorithm |
| 178 | + |
| 179 | +1. Initialize two empty lists/arrays: one for even integers and one for odd integers. |
| 180 | +2. Iterate through the given array: |
| 181 | + - If the current integer is even, add it to the even list/array. |
| 182 | + - If the current integer is odd, add it to the odd list/array. |
| 183 | +3. Concatenate the even list/array with the odd list/array. |
| 184 | +4. Return the concatenated list/array. |
| 185 | + |
| 186 | +## Conclusion |
| 187 | + |
| 188 | +This problem can be solved efficiently by iterating through the array once and separating the integers into even and odd lists/arrays. The time complexity is O(n), where n is the length of the array, making this approach optimal for the given constraints. |
0 commit comments