Find common elements in 3 sorted arrays | Q19 | Love Babbar DSA Sheet | leetcode | Best Approach

Sdílet
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

Komentáře • 85

  • @deepaksarvepalli2344
    @deepaksarvepalli2344 Před 3 lety +6

    Today i felt that i am not waste in solving problems cause i too think same approaches before watching the vedio

  • @nayanbramhane8077
    @nayanbramhane8077 Před 5 měsíci

    nice explanation

  • @shellindebted5328
    @shellindebted5328 Před 2 lety

    Thank you Sir for the wonderful explanation. It helped me a lot.

  • @halchal41
    @halchal41 Před 2 lety

    You are doing a great job !!

  • @sudhirmallick1716
    @sudhirmallick1716 Před 2 lety

    thanx .... nice explanation

  • @SushilaDevi-zp9le
    @SushilaDevi-zp9le Před 3 lety

    Awesome explanation 👌

  • @lokeshnegi5051
    @lokeshnegi5051 Před 3 lety

    great work bro,you made it really easy to understand

  • @imranwahid9871
    @imranwahid9871 Před 3 lety

    Thanks buddy

  • @rajput_warriors5108
    @rajput_warriors5108 Před 2 lety +1

    we can also store first the common elements in set and then convert in vector that reduces time complexity minutely

  • @hardikpandey5349
    @hardikpandey5349 Před 3 lety

    Great explanation

  • @yuktikashyap3374
    @yuktikashyap3374 Před rokem +2

    For Java u can keep a chk😅
    Like
    while(i-1>-1 && i

  • @mohammedwaseem8599
    @mohammedwaseem8599 Před 3 lety +15

    Bro i wrote the same code in c++ it's giving TLE or output has -1

  • @divyanshuchaudhari5416
    @divyanshuchaudhari5416 Před 3 lety +10

    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.

  • @ashwinvarma9349
    @ashwinvarma9349 Před 3 lety

    nice explanation!

  • @meveaan
    @meveaan Před 2 lety

    while loop has time complexity of O(nlogn)

  • @ayushvats1808
    @ayushvats1808 Před 2 lety +2

    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

    • @tusharnain6652
      @tusharnain6652 Před 2 lety

      that checking of element will cause more o(n) time

    • @triptitiwari7533
      @triptitiwari7533 Před rokem

      i tried that way before watching this lecture and it was giving runtime error i dont even know why..

  • @hariprasadcr5936
    @hariprasadcr5936 Před 2 lety +15

    Isn't the "int xx = a[i-1]" is faulty when i=0, (similarly for j and k) ?

    • @tusharnain6652
      @tusharnain6652 Před 2 lety +3

      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.

    • @kushagraagrawal9346
      @kushagraagrawal9346 Před 2 lety

      @@tusharnain6652 garbage and value at 0 position same bhi toh ho skti hai, vo dikkt degi..

  • @AkashGupta-zd1tz
    @AkashGupta-zd1tz Před 5 měsíci

    Two hash se bhi kaam hoojata

  • @vishalmishra7719
    @vishalmishra7719 Před 3 lety +3

    It shows array index out of bound if run java code

    • @itstimetofit8600
      @itstimetofit8600 Před 3 lety +3

      just exchange the while loops with :
      while((i>0 && i0 && j0 && k

  • @rishabsharma5307
    @rishabsharma5307 Před 2 lety

    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)

    • @aryangupta5586
      @aryangupta5586 Před 2 lety +8

      unordered maps has o(1) operations and map has o(log n) operations

  • @divyanshuchaudhari5416
    @divyanshuchaudhari5416 Před 3 lety +2

    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

  • @roshankonj2569
    @roshankonj2569 Před rokem

    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++;
    }

    • @tarunbehera934
      @tarunbehera934 Před rokem

      where do I add this code? like after getting 3 3 3 as O/P,
      I should add this code??

    • @lalit-singh-bisht
      @lalit-singh-bisht Před rokem

      @@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

  • @shreem3461
    @shreem3461 Před 3 lety +4

    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

    • @saikatmukherjee8095
      @saikatmukherjee8095 Před 3 lety +1

      lean java for deep web d and android and c++is best for ds algo

    • @halfaddercircuit
      @halfaddercircuit Před 2 lety

      @@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.

  • @saiquakhan3850
    @saiquakhan3850 Před 3 lety

    Pls upload 2-3 videos in a day

    • @CodeLibrary
      @CodeLibrary  Před 3 lety +1

      Yaa we are uploading 2 videos per day...but today was some technical issues so we managed to upload 1 video.

    • @saiquakhan3850
      @saiquakhan3850 Před 3 lety

      @@CodeLibrary ok tysm

  • @lalit-singh-bisht
    @lalit-singh-bisht Před rokem

    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]....

  • @dishadugad8675
    @dishadugad8675 Před 2 lety +1

    what if i or j or k remains 0 then we will compare with -1th element

  • @rahulscapes
    @rahulscapes Před 9 měsíci

    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

    • @rahulscapes
      @rahulscapes Před 9 měsíci

      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

  • @Aryansingh-fk7hy
    @Aryansingh-fk7hy Před 2 lety

    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.

  • @techwithrajdeep7633
    @techwithrajdeep7633 Před 3 lety

    its giving me errors when I am solving it in gfg same your solution. I don't know if its a compiler issue

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety +1

    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) ?

  • @kaushalsheth9185
    @kaushalsheth9185 Před 3 lety +1

    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?

    • @deveshdubey6776
      @deveshdubey6776 Před 3 lety +1

      @Vaibhav Vijayvargiya i"m too late..but bro i didn't get TLE..its accepted

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety

    also for the second approach we are getting index out of bounds error, you didnt handle it?

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety

    at 4:37 ,first appraoch you didnt consider case of -1?

  • @user-vt1cu1rx7v
    @user-vt1cu1rx7v Před rokem

    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)?

    • @lalit-singh-bisht
      @lalit-singh-bisht Před rokem

      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

  • @Southground
    @Southground Před 3 lety

    It shows Array Index out of bounds

  • @nott__aceu4857
    @nott__aceu4857 Před 2 lety

    what if there are repeated elements in one array

  • @sunnyvlogs__
    @sunnyvlogs__ Před 3 lety

    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.

    • @eshanhembrom6633
      @eshanhembrom6633 Před 3 lety +1

      It will not work if a certain element is repeated in any array

    • @AnkitSingh-wq2rk
      @AnkitSingh-wq2rk Před 3 lety +1

      @@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 ?

    • @rishabsharma5307
      @rishabsharma5307 Před 2 lety +1

      @@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;
      }

    • @vishalplayzz2580
      @vishalplayzz2580 Před rokem

      @@rishabsharma5307 nice soln bhai

  • @baigankabharta6478
    @baigankabharta6478 Před 3 lety +5

    first approach can also be solved using a single hashmap that way space complexity will reduced to O(n1) only

    • @lokeshnegi5051
      @lokeshnegi5051 Před 3 lety

      no it wont be possible bcoz the array also contains duplicate element.

    • @rishabsharma5307
      @rishabsharma5307 Před 2 lety

      @@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;
      }

  • @farazahmed7
    @farazahmed7 Před 2 lety

    element HAI. b mai HAI. c chota HAI.

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety

    in the second appraoch also you didnt handle the case of -1

  • @vanshgoyal1356
    @vanshgoyal1356 Před 2 lety

    Can someone please explain return -1 is not working instead he used return {-1} what difference does the {} makes

    • @naveenjain243
      @naveenjain243 Před rokem

      As the function wants us to return a vector not an integer. We used { } to return a vector with size 1 and value -1

  • @raghavsingh6156
    @raghavsingh6156 Před 3 lety +1

    Java mai jisko krna h??

  • @melvinkimathi8924
    @melvinkimathi8924 Před 3 lety

    could you consider using English

  • @sudhanshoosarage3734
    @sudhanshoosarage3734 Před 2 lety +2

    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

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety

    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

  • @user-rl5sl8zr8w
    @user-rl5sl8zr8w Před 10 měsíci

    First solution was totally wrong..

  • @ankurraj2531
    @ankurraj2531 Před 2 lety

    3 3 3
    3 2 3
    3 2 3
    3 2 3
    ??? Your code wrong answer.