Find common elements in 3 sorted arrays | Q19 | Love Babbar DSA Sheet | leetcode | Best Approach
Vložit
- čas přidán 6. 11. 2020
- #competitiveprogramming #dsasheet #interviewpreparation
In this video I have solved the 19th problem of the sheet.
Love Babbar DSA Sheet : drive.google.com/file/d/1FMdN...
Hope you like it. Comment if you have any doubt.
LIKE | SHARE | SUBSCRIBE
Today i felt that i am not waste in solving problems cause i too think same approaches before watching the vedio
same bro
nice explanation
Thank you Sir for the wonderful explanation. It helped me a lot.
You are doing a great job !!
thanx .... nice explanation
Awesome explanation 👌
great work bro,you made it really easy to understand
Thanks buddy
we can also store first the common elements in set and then convert in vector that reduces time complexity minutely
Great explanation
For Java u can keep a chk😅
Like
while(i-1>-1 && i
thanks bro
Bro i wrote the same code in c++ it's giving TLE or output has -1
Insert -1 in vector then return
Surprised how is not getting index out of bound error for int xx=a[i-1] when i=0 and not incremented in the loops above it. Looks like some problem with GFG test cases.
now it's showing
you won't get bound error, you'll get a garbage value;
nice explanation!
Thanks! 😃
while loop has time complexity of O(nlogn)
in the second method instead of using all those 3 loops at end we can simply add a check that if the elements once get added to our answer vector , don't add it again if(A[i] not present in vector) then only add
that checking of element will cause more o(n) time
i tried that way before watching this lecture and it was giving runtime error i dont even know why..
Isn't the "int xx = a[i-1]" is faulty when i=0, (similarly for j and k) ?
no, that's not it, when i = 0, xx will be a[-1], c++ compiler return garbage value in that case, xx will contain garbage value, you can try to cout it.
@@tusharnain6652 garbage and value at 0 position same bhi toh ho skti hai, vo dikkt degi..
Two hash se bhi kaam hoojata
It shows array index out of bound if run java code
just exchange the while loops with :
while((i>0 && i0 && j0 && k
I think the time complexity of the first solution will be O(N log N) because insertion in map takes O(log N) complexity and for n elements, it'll be O(N log N)
unordered maps has o(1) operations and map has o(log n) operations
Java Solution:
class Solution
{
ArrayList commonElements(int A[], int B[], int C[], int n1, int n2, int n3)
{
int x = 0;
int y = 0;
int z = 0;
ArrayList result = new ArrayList();
while(x
Simple logic to store unique elements as the given are all sorted, we can check last element present in our ans vector is already added or not.
if((a[i] == b[j]) && (a[i] == c[k]))]
{
if(ans.empty() || ans.back() != a[i])
ans.push_back(a[i]);
i++; j++; k++;
}
where do I add this code? like after getting 3 3 3 as O/P,
I should add this code??
@@tarunbehera934
vector commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
{
vectorans;
int i=0,j=0,k=0;
while(i
Is c++ language more concise than java?? I was trying to solve this programs in java but it was taking so much of tym to understand things in java but after seeing ur vids i switch to c++.What should i do i also want to learn java
lean java for deep web d and android and c++is best for ds algo
@@saikatmukherjee8095 Thats not true actually. You can use pretty much any language for DSA. For over two years now I have been using Javascript for DSA.
Pls upload 2-3 videos in a day
Yaa we are uploading 2 videos per day...but today was some technical issues so we managed to upload 1 video.
@@CodeLibrary ok tysm
bhai ismein xx = a[i-1] yy=b[j-1] zz=c[k-1] , toh isss question mein why the it's not giving segment fault, since when i=0,j=0,k=0 then it will try of access a[-1], b[-1].c[-1]....
what if i or j or k remains 0 then we will compare with -1th element
Chup bi 🤣🤣🤣
I dont know about C++ but in python, we check if the A[i] is already in Answer array, if not we append, else we leave it alone, I will attach the source code in reply
def commonElements (self,A, B, C, n1, n2, n3):
# your code here
i , j ,k = 0,0,0
answer = []
while i < n1 and j < n2 and k < n3:
if A[i] == B[j] and B[j] == C[k]:
if A[i] not in answer:
answer.append(A[i])
i+=1
j+=1
k+=1
elif A[i] < B[j]:
i+=1
elif B[j] < C[k]:
j+=1
else:
k+=1
return answer
giving index out of range at x=ar1[i-1],then the consecutive loop defined for the same.Help me with this.GFG code for python has not mentioned this condition.
its giving me errors when I am solving it in gfg same your solution. I don't know if its a compiler issue
at 8:47 ,isnt the time complexity of this approach o(k^2) ,where k = n1+ n2+ n3 ? you are using two while loops, how is it O(k) ?
The latter part of the code where you try to eliminate duplicate entires in v seems to be problematic. Dont those 3 while loops need another terminating conditions?
@Vaibhav Vijayvargiya i"m too late..but bro i didn't get TLE..its accepted
also for the second approach we are getting index out of bounds error, you didnt handle it?
at 4:37 ,first appraoch you didnt consider case of -1?
Hello i am new to this.
Can someone explain that in second method we have used vector that can take n size at worst case but how is the space complexity of second method is O(1)?
because when need a vector to return from that vector function...we didn't use the vector while writing the algorithm of the problem...so if the function only requires you to return vector or even vector of vector then that extra space don't acccumulates the space complexity.... I don't know if you get it, because of my weak grammar
It shows Array Index out of bounds
what if there are repeated elements in one array
can we do the above problem using one hashmap ,
like hashing all the arrays and then traversing and seeing if there value==3 , if yes then print.
It will not work if a certain element is repeated in any array
@@eshanhembrom6633 agar ham har element ki freq fix karde toh ? ans mein waise bhi repeating element ko count nhi karna hai (3,3,3) wala example
toh A array ke element ko check karskte hai agar woh map mein pehele se hi freq 1 hai toh skip , aise B array mein agar map mein woh 2 hai toh skip , and C array meiin agar woh element mein map mein pehele se hi 3 hai toh skip tabhi bhi o(n1) nahi hoga ?
@@AnkitSingh-wq2rk ye lo bhai
vector commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
{
map mp;
vector vect;
for(int i = 0;i < n1; i++)
mp[A[i]] = 1;
for(int i = 0; i < n2; i++)
{
if(mp[B[i]])
mp[B[i]] = 2;
}
for(int i = 0;i < n3; i++)
{
if(mp[C[i]] == 2)
mp[C[i]] = 3;
}
for(auto itr = mp.begin(); itr != mp.end(); itr++)
{
if(itr->second == 3)
vect.push_back(itr->first);
}
return vect;
}
@@rishabsharma5307 nice soln bhai
first approach can also be solved using a single hashmap that way space complexity will reduced to O(n1) only
no it wont be possible bcoz the array also contains duplicate element.
@@lokeshnegi5051 Everything is possible. It takes care of duplicates too
vector commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
{
map mp;
vector vect;
for(int i = 0;i < n1; i++)
mp[A[i]] = 1;
for(int i = 0; i < n2; i++)
{
if(mp[B[i]] == 1)
mp[B[i]] = 2;
}
for(int i = 0;i < n3; i++)
{
if(mp[C[i]] == 2)
mp[C[i]] = 3;
}
for(auto itr = mp.begin(); itr != mp.end(); itr++)
{
if(itr->second == 3)
vect.push_back(itr->first);
}
return vect;
}
element HAI. b mai HAI. c chota HAI.
in the second appraoch also you didnt handle the case of -1
Can someone please explain return -1 is not working instead he used return {-1} what difference does the {} makes
As the function wants us to return a vector not an integer. We used { } to return a vector with size 1 and value -1
Java mai jisko krna h??
solved ?
could you consider using English
Update last else condition
write:
elser if( C[k] < A[i] ){
k++;
}
Above code will not work for Input:
1,2,3
1,2,3
1,2,3
in java, i did the same logic, without line no.32, it is working fine, i dont understand why it is not working for c++
i used hashmap
use hash instead
First solution was totally wrong..
3 3 3
3 2 3
3 2 3
3 2 3
??? Your code wrong answer.
bhai array sorted h