Skip to content

Commit 1066680

Browse files
Create 0905-Sort-Array-By-Parity.md
Add solution to the leetcode problem 905 - Sort Array By Parity by using Python, Java, C++, C and JavaScript
1 parent c821593 commit 1066680

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
id: Sort-Array-By-Parity
3+
title: Sort Array By Parity
4+
sidebar_label: Sort Array By Parity
5+
tags:
6+
- Arrays
7+
- Sorting
8+
---
9+
10+
## Problem Description
11+
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/) |
15+
16+
## Problem Description
17+
18+
Given an integer array `nums`, move all the even integers at the beginning of the array followed by all the odd integers.
19+
20+
Return any array that satisfies this condition.
21+
22+
### Example 1:
23+
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.
27+
28+
### Example 2:
29+
30+
**Input:** `nums = [0]`
31+
**Output:** `[0]`
32+
33+
## Constraints
34+
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.
42+
43+
## Solution
44+
45+
### Python
46+
47+
```python
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]
56+
```
57+
58+
### Java
59+
60+
```java
61+
import java.util.*;
62+
63+
public class EvenOddArray {
64+
public static int[] sortArrayByParity(int[] nums) {
65+
List<Integer> even = new ArrayList<>();
66+
List<Integer> odd = new ArrayList<>();
67+
68+
for (int num : nums) {
69+
if (num % 2 == 0) {
70+
even.add(num);
71+
} else {
72+
odd.add(num);
73+
}
74+
}
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);
100+
}
101+
}
102+
even.insert(even.end(), odd.begin(), odd.end());
103+
return even;
104+
}
105+
106+
int main() {
107+
std::vector<int> nums = {3, 1, 2, 4};
108+
std::vector<int> result = sortArrayByParity(nums);
109+
for (int num : result) {
110+
std::cout << num << " ";
111+
}
112+
// Output: 2 4 3 1
113+
}
114+
```
115+
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];
131+
}
132+
}
133+
*returnSize = numsSize;
134+
for (int i = 0; i < numsSize; ++i) {
135+
nums[i] = result[i];
136+
}
137+
free(result);
138+
}
139+
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]);
148+
}
149+
// Output: 2 4 3 1
150+
return 0;
151+
}
152+
```
153+
154+
### JavaScript
155+
156+
```javascript
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+
}
167+
}
168+
169+
return [...even, ...odd];
170+
}
171+
172+
// Example usage
173+
let nums = [3, 1, 2, 4];
174+
console.log(sortArrayByParity(nums)); // Output: [2, 4, 3, 1]
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

Comments
 (0)