Find common elements in three sorted arrays | GeeksforGeeks
Вставка
- Опубліковано 23 кві 2017
- GeeksforGeeks Article: www.geeksforgeeks.org/find-com...
Practice Problem: practice.geeksforgeeks.org/pro...
This video is contributed by Aditi Bainss
Please Like, Comment, Share the Video among your friends.
Also, Subscribe if you haven't already! :)
good one there....
Audio is too low...
Bhushan Dhodi Yes and suddenly it increases and decreases
I don't remember when I comment that
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
Actual logically the solution is correct, it is printing all the common elements from each arrays.
@@prajwalurkude007 but we have to handle all the cases
@@K_EC_piyushkumarsingh Use TreeSet to store ans and at the end use list.addAll(treeset) to put non duplicates in list
@@deepanshugrover2672 we don't have to use any additional data structure for maintaining dublicates it's mentioned
if u r using ArrayList to store ans then before adding the element check if it exists in the list
🙂
class Solution {
ArrayList commonElements(int A[], int B[], int C[], int n1, int n2, int n3) {
ArrayList 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]
duplicates are not managed I think
def findCommonelements(arr1,arr2,arr3):
for i in arr3:
if i in arr2 and arr1:
print(i)
And the Time complexity becomes
N3 * (N1 + N2)
How time complexity is O( n1+ n2+ n3 )?
order of n basically
Bhai thoda zoor se bola kro
poor explaination
Bhaut tatti explanation diye HO GFK
haha