Skip to content

Commit b73e993

Browse files
authored
Merge branch 'main' into main
2 parents 25b5f78 + a5da52b commit b73e993

File tree

5 files changed

+472
-3
lines changed

5 files changed

+472
-3
lines changed

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
contact_links:
2+
- name: CodeHarborHub Q&A
3+
url: https://codeharborhub.github.io/community/supports#-how-to-get-support
4+
about: Get help from the community with any questions or issues.
5+
6+
- name: CodeHarborHub Features
7+
url: https://codeharborhub.github.io/community/features
8+
about: Explore the features and capabilities of CodeHarborHub.
9+
10+
- name: Meet the Founder or Owner
11+
url: https://codeharborhub.github.io/me/
12+
about: Learn more about the founder and owner of CodeHarborHub.
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
id: find-duplicates-in-an-array
3+
title: Find Duplicates in an Array (gfg)
4+
sidebar_label: 0006 - Find Duplicates in an Array
5+
tags:
6+
- Easy
7+
- Array
8+
- GeeksforGeeks
9+
- CPP
10+
- Python
11+
- DSA
12+
description: "This tutorial covers the solution to the Find Duplicates in an Array problem from the GeeksforGeeks website, featuring implementations in Python and C++."
13+
---
14+
15+
## Problem Description
16+
17+
Given an array `arr` of size `n` which contains elements in the range from 0 to `n-1`, you need to find all the elements occurring more than once in the given array. Return the answer in ascending order. If no such element is found, return a list containing [-1].
18+
19+
:::note
20+
Try and perform all operations within the provided array. The extra (non-constant) space needs to be used only for the array to be returned.
21+
:::
22+
## Examples
23+
24+
**Example 1:**
25+
26+
```
27+
Input: arr = [2, 3, 1, 2, 3]
28+
Output: [2, 3]
29+
Explanation: 2 and 3 occur more than once in the array.
30+
```
31+
32+
**Example 2:**
33+
34+
```
35+
Input: arr = [0, 1, 2, 3]
36+
Output: [-1]
37+
Explanation: No element occurs more than once.
38+
```
39+
40+
## Your Task
41+
42+
You don't need to read input or print anything. Your task is to complete the function `duplicates()` which takes the array `arr` as input and returns a list of the duplicate elements in ascending order.
43+
44+
Expected Time Complexity: $O(n)$
45+
46+
Expected Auxiliary Space: $O(1)$ (excluding the space for the output list)
47+
48+
## Constraints
49+
50+
* `1 ≤ n ≤ 10^5`
51+
* `0 ≤ arr[i] ≤ n-1`
52+
53+
## Problem Explanation
54+
55+
The problem is to find the duplicate elements in an array of size `n`, where the elements range from 0 to `n-1`. The duplicates should be returned in ascending order. If no duplicates are found, return [-1].
56+
57+
## Code Implementation
58+
59+
<Tabs>
60+
<TabItem value="Python" label="Python" default>
61+
<SolutionAuthor name="@YourUsername"/>
62+
63+
```py
64+
class Solution:
65+
def duplicates(self, arr):
66+
n = len(arr)
67+
# Use the array elements as index
68+
for i in range(n):
69+
arr[arr[i] % n] += n
70+
71+
# Collect elements that occur more than once
72+
result = [i for i in range(n) if arr[i] // n > 1]
73+
74+
return result if result else [-1]
75+
76+
# Example usage
77+
if __name__ == "__main__":
78+
solution = Solution()
79+
print(solution.duplicates([2, 3, 1, 2, 3])) # Expected output: [2, 3]
80+
print(solution.duplicates([0, 1, 2, 3])) # Expected output: [-1]
81+
```
82+
83+
</TabItem>
84+
<TabItem value="C++" label="C++">
85+
<SolutionAuthor name="@YourUsername"/>
86+
87+
```cpp
88+
#include <iostream>
89+
#include <vector>
90+
#include <algorithm>
91+
using namespace std;
92+
93+
class Solution {
94+
public:
95+
vector<int> duplicates(vector<long long>& arr) {
96+
int n = arr.size();
97+
vector<int> result;
98+
99+
// Use the array elements as index
100+
for (int i = 0; i < n; i++) {
101+
arr[arr[i] % n] += n;
102+
}
103+
104+
// Collect elements that occur more than once
105+
for (int i = 0; i < n; i++) {
106+
if (arr[i] / n > 1) {
107+
result.push_back(i);
108+
}
109+
}
110+
111+
if (result.empty()) {
112+
return {-1};
113+
}
114+
115+
return result;
116+
}
117+
};
118+
119+
// Example usage
120+
void solve() {
121+
int n;
122+
cin >> n;
123+
vector<long long> arr(n);
124+
for (int i = 0; i < n; i++) {
125+
cin >> arr[i];
126+
}
127+
128+
Solution obj;
129+
vector<int> ans = obj.duplicates(arr);
130+
for (int i : ans) {
131+
cout << i << ' ';
132+
}
133+
cout << endl;
134+
}
135+
136+
int main() {
137+
int t;
138+
cin >> t;
139+
140+
while (t--) {
141+
solve();
142+
}
143+
return 0;
144+
}
145+
```
146+
147+
</TabItem>
148+
</Tabs>
149+
150+
## Example Walkthrough
151+
152+
For the array `arr = [2, 3, 1, 2, 3]`:
153+
154+
1. Iterate through the array and use each element as an index. Increase the value at that index by `n` to mark the occurrence.
155+
2. After the iteration, elements with values greater than `2 * n` indicate duplicates.
156+
3. Collect such elements and return them in ascending order.
157+
158+
For the array `arr = [0, 1, 2, 3]`:
159+
160+
1. Iterate through the array and use each element as an index. Increase the value at that index by `n` to mark the occurrence.
161+
2. After the iteration, no elements have values greater than `2 * n`.
162+
3. Return [-1] since no duplicates are found.
163+
164+
## Solution Logic:
165+
166+
1. Iterate through the array, using each element as an index.
167+
2. Increase the value at the calculated index by `n`.
168+
3. After the iteration, check which elements have values greater than `2 * n` to find duplicates.
169+
4. Collect such elements and return them in ascending order.
170+
171+
## Time Complexity
172+
173+
* The time complexity is $O(n)$, where n is the size of the input array.
174+
175+
## Space Complexity
176+
177+
* The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input array.
178+
179+
## References
180+
181+
- **gfg Problem:** [gfg Problem](https://www.geeksforgeeks.org/problems/find-duplicates-in-an-array/1?page=1&difficulty=Easy&sortBy=submissions)
182+
- **Solution Author:** [arunimad6yuq](https://www.geeksforgeeks.org/user/arunimad6yuq/)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
id: find-the-missing-number
3+
title: Find the Missing Number Problem (gfg)
4+
sidebar_label: 0005 - Find the Missing Number
5+
tags:
6+
- Intermediate
7+
- Array
8+
- Mathematical
9+
- GeeksforGeeks
10+
- CPP
11+
- Python
12+
- DSA
13+
description: "This tutorial covers the solution to the Find the Missing Number problem from the GeeksforGeeks website, featuring implementations in Python and C++."
14+
---
15+
## Problem Description
16+
17+
Given an array containing `n-1` distinct numbers taken from 1 to `n`, find the one number that is missing from the array.
18+
19+
## Examples
20+
21+
**Example 1:**
22+
23+
```
24+
Input: array = [3, 1, 4]
25+
Output: 2
26+
Explanation: The missing number is 2.
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: array = [5, 3, 1, 2]
33+
Output: 4
34+
Explanation: The missing number is 4.
35+
```
36+
37+
## Your Task
38+
39+
You don't need to read input or print anything. Your task is to complete the function `missingNumber()` which takes the size `n` of the range and the array `arr` as inputs and returns the missing number.
40+
41+
Expected Time Complexity: $O(n)$
42+
43+
Expected Auxiliary Space: $O(1)$
44+
45+
## Constraints
46+
47+
* `2 ≤ n ≤ 10^6`
48+
* `1 ≤ array[i] ≤ n`
49+
* All elements of the array are distinct.
50+
51+
## Problem Explanation
52+
53+
The problem is to find the missing number from an array of `n-1` elements which contains distinct numbers from 1 to `n`. The missing number is the one number that is not present in the array.
54+
55+
## Code Implementation
56+
57+
<Tabs>
58+
<TabItem value="Python" label="Python" default>
59+
<SolutionAuthor name="@YourUsername"/>
60+
61+
```py
62+
class Solution:
63+
def missingNumber(self, n, arr):
64+
# Calculate the expected sum of the first n natural numbers
65+
total_sum = n * (n + 1) // 2
66+
67+
# Calculate the sum of the elements in the array
68+
arr_sum = sum(arr)
69+
70+
# The missing number is the difference between the expected sum and the array sum
71+
return total_sum - arr_sum
72+
73+
# Example usage
74+
if __name__ == "__main__":
75+
solution = Solution()
76+
print(solution.missingNumber(4, [3, 1, 4])) # Expected output: 2
77+
print(solution.missingNumber(5, [5, 3, 1, 2])) # Expected output: 4
78+
```
79+
80+
</TabItem>
81+
<TabItem value="C++" label="C++">
82+
<SolutionAuthor name="@YourUsername"/>
83+
84+
```cpp
85+
#include <iostream>
86+
#include <vector>
87+
using namespace std;
88+
89+
class Solution {
90+
public:
91+
int missingNumber(int n, vector<int>& arr) {
92+
// Calculate the expected sum of the first n natural numbers
93+
int total_sum = n * (n + 1) / 2;
94+
95+
// Calculate the sum of the elements in the array
96+
int arr_sum = 0;
97+
for (int num : arr) {
98+
arr_sum += num;
99+
}
100+
101+
// The missing number is the difference between the expected sum and the array sum
102+
return total_sum - arr_sum;
103+
}
104+
};
105+
106+
// Example usage
107+
int main() {
108+
int t;
109+
cin >> t;
110+
while (t--) {
111+
int n;
112+
cin >> n;
113+
114+
vector<int> arr(n - 1);
115+
for (int i = 0; i < n - 1; ++i)
116+
cin >> arr[i];
117+
Solution obj;
118+
cout << obj.missingNumber(n, arr) << "\n";
119+
}
120+
return 0;
121+
}
122+
```
123+
124+
</TabItem>
125+
</Tabs>
126+
127+
## Example Walkthrough
128+
129+
For the array `array = [3, 1, 4]` with `n = 4`:
130+
131+
1. The expected sum of the first 4 natural numbers is `4 * (4 + 1) / 2 = 10`.
132+
2. The sum of the array elements is `3 + 1 + 4 = 8`.
133+
3. The missing number is `10 - 8 = 2`.
134+
135+
For the array `array = [5, 3, 1, 2]` with `n = 5`:
136+
137+
1. The expected sum of the first 5 natural numbers is `5 * (5 + 1) / 2 = 15`.
138+
2. The sum of the array elements is `5 + 3 + 1 + 2 = 11`.
139+
3. The missing number is `15 - 11 = 4`.
140+
141+
## Solution Logic:
142+
143+
1. Calculate the expected sum of the first `n` natural numbers using the formula `n * (n + 1) / 2`.
144+
2. Calculate the sum of the elements in the given array.
145+
3. The missing number is the difference between the expected sum and the sum of the array elements.
146+
147+
## Time Complexity
148+
149+
* The time complexity is $O(n)$, where n is the size of the input array.
150+
151+
## Space Complexity
152+
153+
* The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input array.
154+
155+
## References
156+
157+
- **gfg Problem:** [gfg Problem](https://www.geeksforgeeks.org/problems/missing-number-in-array1416/1?page=1&difficulty=Easy&sortBy=submissions)
158+
- **Solution Author:** [arunimad6yuq](https://www.geeksforgeeks.org/user/arunimad6yuq/)

0 commit comments

Comments
 (0)