Skip to content

Commit 563466d

Browse files
authored
Merge branch 'main' into teamcard
2 parents 6f52bc7 + 470b689 commit 563466d

19 files changed

+1581
-22
lines changed

dsa-problems/gfg-problems/easy/0101-0200.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const problems = [
2525
{
2626
"difficulty": "Easy",
2727
"gfgLink": "https://www.geeksforgeeks.org/problems/index-of-an-extra-element/1",
28-
"solutionLink": "#",
28+
"solutionLink": "/dsa-solutions/gfg-solutions/easy/index-of-an-extra-element",
2929
"problemName": "Index of an Extra Element"
3030
},
3131
{
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
id: index-of-an-extra-element
3+
title: Index of an Extra Element
4+
sidebar_label: 0103-Index of an Extra Element
5+
6+
tags:
7+
- arrays
8+
- searching
9+
- data structures
10+
- algorithms
11+
description: "A solution to the problem of determining the index of the extra element in the array "
12+
---
13+
14+
In this page, we will solve the problem of determining the index of the extra element in the array.
15+
16+
## Problem Description
17+
18+
You have given two sorted arrays arr1[] & arr2[] of distinct elements. The first array has one element extra added in between. Return the index of the extra element.
19+
20+
Note: 0-based indexing is followed.
21+
22+
### Examples
23+
24+
**Example 1:**
25+
26+
```plaintext
27+
Input: n = 7, arr1[] = {2,4,6,8,9,10,12}, arr2[] = {2,4,6,8,10,12}
28+
29+
Output: 4
30+
31+
Explanation: In the first array, 9 is extra added and it's index is 4.
32+
```
33+
34+
**Example 2:**
35+
36+
```plaintext
37+
Input: n = 6, arr1[] = {3,5,7,8,11,13}, arr2[] = {3,5,7,11,13}
38+
39+
Output: 3
40+
41+
Explanation: In the first array, 8 is extra and it's index is 3.
42+
```
43+
44+
### Constraints
45+
46+
- $1 \leq N \leq10^5$, where $N$ is number of nodes
47+
- $1 \leq arr1[i], arr2[i] \leq 10^6$
48+
## Solution
49+
50+
### Intuition and Approach
51+
52+
This problem can be solved by iterating over the array by runnig the loop from o to the length of the smallest array
53+
<Tabs>
54+
<tabItem value="Inorder Traversal" label="Inorder Traversal">
55+
56+
### Approach: Inorder Traversal
57+
58+
1. Run a for loop from 0 to length of the smallest array.
59+
2. If at any point in loop the element on ith index of both arrays are different then return the index as it is the answer
60+
3. If loop runs completely and didn't terminate this means the last index of the largest array is the answer and return that index
61+
62+
#### Code in Python
63+
```python
64+
class Solution:
65+
def findExtra(self,n,a,b):
66+
for i in range(min(len(a),len(b))):
67+
if a[i]!=b[i]:return i
68+
return i+1
69+
```
70+
71+
#### Complexity Analysis
72+
73+
- **Time Complexity:** $O(N)$
74+
- **Space Complexity:** $O(1)$
75+
76+
</tabItem>
77+
</Tabs>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
id: transpose-file
3+
title: Transpose File
4+
difficulty: Medium
5+
sidebar_label: 0194-Transpose-File
6+
tags:
7+
- Shell
8+
- LeetCode Medium
9+
---
10+
11+
## Problem Description
12+
Given a text file `file.txt`, transpose its content.
13+
14+
You may assume that each row has the same number of columns and each field is separated by the `' '` character.
15+
16+
### Example
17+
**Example 1:**
18+
```plaintext
19+
Assume that `file.txt` has the following content:
20+
name age
21+
alice 21
22+
ryan 30
23+
24+
Your script should output the following transposed content:
25+
name alice ryan
26+
age 21 30
27+
```
28+
29+
### Constraints
30+
- The file `file.txt` will be at most 10^3 lines.
31+
- Each line in the file will have at most 10^3 characters.
32+
- Each field in the file is separated by a single space character.
33+
34+
## Solution Approach
35+
36+
### Approach Overview
37+
We can use a combination of `awk`, `paste`, and `cut` commands in a bash script to transpose the content of the file.
38+
39+
### Detailed Steps
40+
41+
1. **Read Each Line and Store Columns**:
42+
- Use `awk` to read each line and store columns in arrays.
43+
44+
2. **Print Transposed Content**:
45+
- Loop through the columns and print them as rows.
46+
47+
## Code Examples
48+
49+
### Shell Script
50+
```sh
51+
awk '
52+
{
53+
for (i = 1; i <= NF; i++) {
54+
if (NR == 1) {
55+
s[i] = $i;
56+
} else {
57+
s[i] = s[i] " " $i;
58+
}
59+
}
60+
}
61+
END {
62+
for (i = 1; i <= NF; i++) {
63+
print s[i];
64+
}
65+
}
66+
' file.txt
67+
```
68+
69+
## Complexity
70+
71+
- **Time Complexity**: `O(n * m)`, where `n` is the number of rows and `m` is the number of columns. Each element is processed once.
72+
73+
- **Space Complexity**: `O(n * m)`, as we store each element in memory to perform the transposition.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
id: tenth-line
3+
title: Tenth Line
4+
difficulty: Easy
5+
sidebar_label: 0195-Tenth-Line
6+
tags:
7+
- Shell
8+
- LeetCode Easy
9+
---
10+
11+
## Problem Description
12+
Given a text file `file.txt`, print just the 10th line of the file.
13+
14+
If the file has less than 10 lines, what should you output? There's no need to output anything (an empty output).
15+
16+
### Example
17+
**Example 1:**
18+
```plaintext
19+
Assume that `file.txt` has the following content:
20+
Line 1
21+
Line 2
22+
Line 3
23+
Line 4
24+
Line 5
25+
Line 6
26+
Line 7
27+
Line 8
28+
Line 9
29+
Line 10
30+
31+
Your script should output the following:
32+
Line 10
33+
```
34+
35+
**Example 2:**
36+
```plaintext
37+
Assume that `file.txt` has the following content:
38+
Line 1
39+
Line 2
40+
Line 3
41+
42+
Your script should output nothing since there are less than 10 lines in the file.
43+
```
44+
45+
### Constraints
46+
- The file `file.txt` will have at most 10^3 lines.
47+
- Each line in the file will have at most 10^3 characters.
48+
49+
## Solution Approach
50+
51+
### Approach Overview
52+
There are multiple ways to achieve this using different shell commands. Here, we demonstrate solutions using `sed`, `awk`, and `tail` with `head`.
53+
54+
### Detailed Steps
55+
56+
1. **Using `sed`**:
57+
- The `sed` command can directly print the 10th line.
58+
59+
2. **Using `awk`**:
60+
- The `awk` command can be used to check the line number and print the 10th line.
61+
62+
3. **Using `tail` and `head`**:
63+
- The `tail` command can be used to get the last lines starting from the 10th, and then `head` can get the first of those lines.
64+
65+
## Code Examples
66+
67+
### Shell Script Using `sed`
68+
```sh
69+
# Using sed to print the 10th line
70+
sed -n '10p' file.txt
71+
```
72+
73+
### Shell Script Using `awk`
74+
```sh
75+
# Using awk to print the 10th line
76+
awk 'NR == 10' file.txt
77+
```
78+
79+
### Shell Script Using `tail` and `head`
80+
```sh
81+
# Using tail and head to print the 10th line
82+
tail -n +10 file.txt | head -n 1
83+
```
84+
85+
## Complexity
86+
87+
- **Time Complexity**: `O(n)`, where `n` is the number of lines in the file. Each line is read once until the 10th line.
88+
89+
- **Space Complexity**: `O(1)`, as no additional space is used beyond the required output.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
id: delete-duplicate-emails
3+
title: Delete Duplicate Emails
4+
difficulty: Easy
5+
sidebar_label: 0196-Delete-Duplicate-Emails
6+
tags:
7+
- SQL
8+
- LeetCode Easy
9+
---
10+
11+
## Problem Description
12+
Write a SQL query to delete all the duplicate emails, keeping only one unique email with the smallest `id`.
13+
14+
Assume the table `Person` has the following structure:
15+
```plaintext
16+
+----+------------------+
17+
| id | email |
18+
+----+------------------+
19+
| 1 | john@example.com |
20+
| 2 | bob@example.com |
21+
| 3 | john@example.com |
22+
+----+------------------+
23+
```
24+
25+
### Example
26+
**Example 1:**
27+
```plaintext
28+
Assume that the `Person` table has the following content:
29+
+----+------------------+
30+
| id | email |
31+
+----+------------------+
32+
| 1 | john@example.com |
33+
| 2 | bob@example.com |
34+
| 3 | john@example.com |
35+
+----+------------------+
36+
37+
Your query should delete the duplicate emails and the result table should look like:
38+
+----+------------------+
39+
| id | email |
40+
+----+------------------+
41+
| 1 | john@example.com |
42+
| 2 | bob@example.com |
43+
+----+------------------+
44+
```
45+
46+
### Constraints
47+
- The `Person` table will have at most 10^4 rows.
48+
- Each email in the `Person` table will have at most 100 characters.
49+
50+
## Solution Approach
51+
52+
### Approach Overview
53+
To delete duplicate emails while keeping the record with the smallest `id`, we can use a subquery to identify the duplicates and then delete them based on their `id`.
54+
55+
### Detailed Steps
56+
57+
1. **Identify Duplicates**:
58+
- Use a subquery to find the smallest `id` for each duplicate email.
59+
60+
2. **Delete Duplicates**:
61+
- Delete from the `Person` table where the `id` is not in the list of smallest `id`s for each email.
62+
63+
## Code Examples
64+
65+
### SQL Query
66+
```sql
67+
DELETE p1 FROM Person p1
68+
JOIN Person p2
69+
ON p1.email = p2.email
70+
AND p1.id > p2.id;
71+
```
72+
73+
### Explanation
74+
- The `JOIN` operation matches each row in the `Person` table with every other row having the same email.
75+
- The `DELETE` statement removes rows from the `Person` table where the `id` is greater than the smallest `id` for each email, effectively keeping only one unique email with the smallest `id`.
76+
77+
## Complexity
78+
79+
- **Time Complexity**: `O(n^2)` in the worst case, where `n` is the number of rows in the `Person` table, due to the `JOIN` operation. However, for practical purposes with indexing and optimizations, the performance may be better.
80+
81+
- **Space Complexity**: `O(1)`, as no additional space is required beyond the query execution.

0 commit comments

Comments
 (0)