Skip to content

Commit 6acf737

Browse files
VanshVansh
Vansh
authored and
Vansh
committed
sync
1 parent cf5e254 commit 6acf737

File tree

2 files changed

+142
-107
lines changed

2 files changed

+142
-107
lines changed

dsa-solutions/gfg-solutions/Easy problems/square-root.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
---
22
id: square-root
33
title: Square Root
4-
sidebar_label: 9 Square Root
4+
sidebar_label: Square-Root
55
tags:
66
- Math
77
- Binary Search
8-
- Python
9-
- Java
10-
- C++
11-
- JavaScript
12-
- TypeScript
13-
description: "This document provides solutions to the problem of finding the square root of a given integer using various programming languages."
8+
description: "This document provides solutions to the problem of finding the Square Root of an integer."
149
---
1510

1611
## Problem
@@ -43,7 +38,7 @@ You don't need to read input or print anything. The task is to complete the func
4338
**Expected Auxiliary Space:** $O(1)$
4439

4540
**Constraints**
46-
- $1 ≤ x ≤ 10^7$
41+
- `1 ≤ x ≤ 10^7`
4742

4843
## Solution
4944

@@ -190,11 +185,4 @@ class Solution {
190185
The provided solutions efficiently find the floor value of the square root of a given integer `x` using binary search. This approach ensures a time complexity of $ O(log N) and an auxiliary space complexity of $O(1)$. The algorithms are designed to handle large values of `x` up to 10^7 efficiently without relying on built-in square root functions.
191186

192187
**Time Complexity:** $O(log N)$
193-
**Auxiliary Space:** $O(1)$
194-
195-
---
196-
197-
## References
198-
199-
- **GeeksforGeeks Problem:** [Square root](https://www.geeksforgeeks.org/problems/square-root/0)
200-
- **Author GeeksforGeeks Profile:** [GeeksforGeeks](https://www.geeksforgeeks.org/user/GeeksforGeeks/)
188+
**Auxiliary Space:** $O(1)$
Lines changed: 138 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,188 @@
11
---
2-
id: sort-array-by-parity
2+
id: Sort-Array-By-Parity
33
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
98
---
109

1110
## Problem Description
1211

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/) |
1415

15-
Return any array that satisfies this condition.
16+
## Problem Description
1617

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.
1819

19-
**Example 1:**
20+
Return any array that satisfies this condition.
2021

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:
2623

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.
2827

29-
```
30-
Input: nums = [0]
31-
Output: [0]
32-
```
28+
### Example 2:
29+
30+
**Input:** `nums = [0]`
31+
**Output:** `[0]`
3332

3433
## Constraints
3534

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.
4042

4143
## Solution
4244

4345
### Python
4446

4547
```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]
5356
```
5457

55-
### C++
58+
### Java
5659

57-
```cpp
58-
#include <vector>
59-
#include <algorithm>
60+
```java
61+
import java.util.*;
6062

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+
6568
for (int num : nums) {
6669
if (num % 2 == 0) {
67-
result.push_back(num);
70+
even.add(num);
71+
} else {
72+
odd.add(num);
6873
}
6974
}
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);
74100
}
75-
return result;
76101
}
77-
};
102+
even.insert(even.end(), odd.begin(), odd.end());
103+
return even;
104+
}
78105

79-
// Example usage:
80106
int main() {
81-
Solution solution;
82107
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
85113
}
86114
```
87115
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];
106131
}
107-
return result.stream().mapToInt(i -> i).toArray();
108132
}
133+
*returnSize = numsSize;
134+
for (int i = 0; i < numsSize; ++i) {
135+
nums[i] = result[i];
136+
}
137+
free(result);
138+
}
109139
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]);
117148
}
149+
// Output: 2 4 3 1
150+
return 0;
118151
}
119152
```
120153

121154
### JavaScript
122155

123156
```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+
}
134167
}
135-
}
136-
return result;
137-
};
168+
169+
return [...even, ...odd];
170+
}
138171

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]
141175
```
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)