Find common elements in three sorted arrays | GeeksforGeeks

preview_player
Показать описание


This video is contributed by Aditi Bainss

Please Like, Comment, Share the Video among your friends.

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

Possibly your code doesn't work correctly for multiple test-cases (TCs).

The first test case where your code failed:

Input:

3 3 3
3 3 3
3 3 3
3 3 3



Its Correct output is:

3



And Your Code's output is:

3 3 3

K_EC_piyushkumarsingh
Автор

python3 easy solution :
class Solution:
def commonElements(self, arr1, arr2, arr3):
ans =
ans.sort()
return ans

chirag
Автор

def findCommonelements(arr1, arr2, arr3):
for i in arr3:
if i in arr2 and arr1:
print(i)

ScrolltheNature
Автор

How time complexity is O( n1+ n2+ n3 )?

akarshmittal
Автор

🙂

class Solution {
ArrayList<Integer> commonElements(int A[], int B[], int C[], int n1, int n2, int n3) {
ArrayList<Integer> result = new ArrayList<>();
int i = 0, j = 0, k = 0;

while (i < n1 && j < n2 && k < n3) {
if (A[i] == B[j] && B[j] == C[k]) {
// Check if the current element is not a duplicate
if (i == 0 || A[i] != A[i - 1]) {
result.add(A[i]);
}
i++;
j++;
k++;
} else if (A[i] <= B[j] && A[i] <= C[k]) {
i++;
} else if (B[j] <= A[i] && B[j] <= C[k]) {
j++;
} else {
k++;
}
}

return result;
}
}

kritikadas