Skip to content

Commit 5318f5f

Browse files
committed
common elemets
1 parent 566f84c commit 5318f5f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

DSA Crack Sheet/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [Count Inversions](https://practice.geeksforgeeks.org/problems/inversion-of-array-1587115620/1# "view question") - [Cpp Solution](./solutions/Count%20Inversions.cpp)
2020
- [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ "view question") - [Cpp Solution](./solutions/Best%20Time%20to%20Buy%20and%20Sell%20Stock.cpp)
2121
- [Count pairs with given sum](https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/1# "view question") - [Cpp Solution](./solutions/Count%20pairs%20with%20given%20sum.cpp)
22+
- [Common elements](https://practice.geeksforgeeks.org/problems/common-elements1132/1# "view question") - [Cpp Solution](./solutions/Common%20elements.cpp)
2223
- []( "view question") - [Cpp Solution](./solutions/.cpp)
2324

2425
<!-- - []( "view question") - [Cpp Solution](./solutions/) -->
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Common elements
3+
===============
4+
5+
Given three arrays sorted in increasing order. Find the elements that are common in all three arrays.
6+
Note: can you take care of the duplicates without using any additional Data Structure?
7+
8+
Example 1:
9+
Input:
10+
n1 = 6; A = {1, 5, 10, 20, 40, 80}
11+
n2 = 5; B = {6, 7, 20, 80, 100}
12+
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120}
13+
Output: 20 80
14+
Explanation: 20 and 80 are the only
15+
common elements in A, B and C.
16+
17+
Your Task:
18+
You don't need to read input or print anything. Your task is to complete the function commonElements() which take the 3 arrays A[], B[], C[] and their respective sizes n1, n2 and n3 as inputs and returns an array containing the common element present in all the 3 arrays in sorted order.
19+
If there are no such elements return an empty array. In this case the output will be printed as -1.
20+
21+
Expected Time Complexity: O(n1 + n2 + n3)
22+
Expected Auxiliary Space: O(n1 + n2 + n3)
23+
24+
Constraints:
25+
1 <= n1, n2, n3 <= 10^5
26+
The array elements can be both positive or negative integers.
27+
*/
28+
29+
vector<int> commonElements(int A[], int B[], int C[], int n1, int n2, int n3)
30+
{
31+
vector<int> ans;
32+
int i = 0, j = 0, k = 0;
33+
34+
while (i < n1 && j < n2 && k < n3)
35+
{
36+
int ma = max(A[i], max(B[j], C[k]));
37+
38+
while (i < n1 && A[i] < ma)
39+
i++;
40+
while (i < n1 - 1 && A[i] == A[i + 1])
41+
i++;
42+
43+
while (j < n2 && B[j] < ma)
44+
j++;
45+
while (j < n2 - 1 && B[j] == B[j + 1])
46+
j++;
47+
48+
while (k < n3 && C[k] < ma)
49+
k++;
50+
while (k < n1 - 1 && C[k] == C[k + 1])
51+
k++;
52+
53+
if (i >= n1 || j >= n2 || k >= n3)
54+
break;
55+
if (A[i] == B[j] && B[j] == C[k])
56+
{
57+
ans.push_back(A[i]);
58+
i++;
59+
j++;
60+
k++;
61+
}
62+
}
63+
64+
return ans;
65+
}

0 commit comments

Comments
 (0)