Skip to content

Commit 020e383

Browse files
authored
Merge pull request #3741 from Ishitamukherjee2004/main
Solution of Common in 3 Sorted Arrays from gfg is added
2 parents 669c591 + e6306ba commit 020e383

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
id: common-in-3-sorted-arrays
3+
title: Common In 3 Sorted Arrays
4+
sidebar_label: Common-In-3-Sorted-Arrays
5+
tags:
6+
- Searching
7+
- Algorithms
8+
description: "This tutorial covers the solution to the Common in 3 sorted arrays problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
12+
You are given three arrays sorted in increasing order. Find the elements that are common in all three arrays.
13+
If there are no such elements return an empty array. In this case, the output will be -1.
14+
15+
## Examples
16+
17+
**Example 1:**
18+
19+
```
20+
Input: arr1 = [1, 5, 10, 20, 40, 80] , arr2 = [6, 7, 20, 80, 100] , arr3 = [3, 4, 15, 20, 30, 70, 80, 120]
21+
Output: [20, 80]
22+
Explanation: 20 and 80 are the only common elements in arr, brr and crr.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: arr1 = [1, 2, 3, 4, 5] , arr2 = [6, 7] , arr3 = [8,9,10]
29+
Output: [-1]
30+
Explanation: There are no common elements in arr, brr and crr.
31+
```
32+
33+
34+
Expected Time Complexity: O(n)
35+
36+
Expected Auxiliary Space: O(n)
37+
38+
## Constraints
39+
40+
* `-10^5 <= arr1i , arr2i , 1arr3i <= 10^5`
41+
42+
## Problem Explanation
43+
44+
The task is to traverse the arrays and find the common element.
45+
46+
## Code Implementation
47+
48+
### C++ Solution
49+
50+
```cpp
51+
52+
#include <vector>
53+
#include <algorithm>
54+
55+
std::vector<int> commonElements(std::vector<int> arr1, std::vector<int> arr2, std::vector<int> arr3) {
56+
std::vector<int> result;
57+
int i = 0, j = 0, k = 0;
58+
while (i < arr1.size() && j < arr2.size() && k < arr3.size()) {
59+
if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) {
60+
result.push_back(arr1[i]);
61+
i++;
62+
j++;
63+
k++;
64+
} else {
65+
if (arr1[i] <= arr2[j] && arr1[i] <= arr3[k]) {
66+
i++;
67+
} else if (arr2[j] <= arr1[i] && arr2[j] <= arr3[k]) {
68+
j++;
69+
} else {
70+
k++;
71+
}
72+
}
73+
}
74+
75+
return result;
76+
}
77+
78+
79+
```
80+
81+
```java
82+
import java.util.ArrayList;
83+
import java.util.List;
84+
85+
public class Main {
86+
public static List<Integer> commonElements(List<Integer> arr1, List<Integer> arr2, List<Integer> arr3) {
87+
List<Integer> result = new ArrayList<>();
88+
int i = 0, j = 0, k = 0;
89+
while (i < arr1.size() && j < arr2.size() && k < arr3.size()) {
90+
if (arr1.get(i) == arr2.get(j) && arr2.get(j) == arr3.get(k)) {
91+
result.add(arr1.get(i));
92+
i++;
93+
j++;
94+
k++;
95+
} else {
96+
if (arr1.get(i) <= arr2.get(j) && arr1.get(i) <= arr3.get(k)) {
97+
i++;
98+
} else if (arr2.get(j) <= arr1.get(i) && arr2.get(j) <= arr3.get(k)) {
99+
j++;
100+
} else {
101+
k++;
102+
}
103+
}
104+
}
105+
106+
return result;
107+
}
108+
}
109+
110+
111+
112+
113+
```
114+
115+
```python
116+
117+
def commonElements(arr1, arr2, arr3):
118+
result = []
119+
i = j = k = 0
120+
121+
while i < len(arr1) and j < len(arr2) and k < len(arr3):
122+
if arr1[i] == arr2[j] == arr3[k]:
123+
result.append(arr1[i])
124+
i += 1
125+
j += 1
126+
k += 1
127+
else:
128+
if arr1[i] <= arr2[j] and arr1[i] <= arr3[k]:
129+
i += 1
130+
elif arr2[j] <= arr1[i] and arr2[j] <= arr3[k]:
131+
j += 1
132+
else:
133+
k += 1
134+
135+
return result
136+
137+
138+
```
139+
140+
```javascript
141+
function commonElements(arr1, arr2, arr3) {
142+
const result = [];
143+
const i = j = k = 0;
144+
145+
while (i < arr1.length && j < arr2.length && k < arr3.length) {
146+
if (arr1[i] === arr2[j] && arr2[j] === arr3[k]) {
147+
result.push(arr1[i]);
148+
i++;
149+
j++;
150+
k++;
151+
} else {
152+
if (arr1[i] <= arr2[j] && arr1[i] <= arr3[k]) {
153+
i++;
154+
} else if (arr2[j] <= arr1[i] && arr2[j] <= arr3[k]) {
155+
j++;
156+
} else {
157+
k++;
158+
}
159+
}
160+
}
161+
162+
return result.length ? result : [-1];
163+
}
164+
165+
166+
```
167+
168+
## Solution Logic:
169+
170+
1. Initialize three pointers, one for each array, at the beginning of the arrays.
171+
2. Compare the elements at the current pointers. If they are equal, add the element to the result array and increment all three pointers.
172+
3. If the elements are not equal, increment the pointer of the array with the smallest current element.
173+
4. Repeat steps 2-3 until one of the arrays is exhausted.
174+
5. Return the result array.
175+
176+
177+
178+
## Time Complexity
179+
180+
* The time complexity is $O(N^2)$ where n is the length of the shortest array. This is because in the worst case, we need to iterate through all elements of the shortest array.
181+
182+
183+
## Space Complexity
184+
185+
* The auxiliary space complexity is $O(m)$ where m is the number of common elements found. This is because we store the common elements in the result array.
186+

0 commit comments

Comments
 (0)