Common elements in 3 sorted arrays | Array | Love Babbar DSA Sheet | Hindi

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • Time Stamps :
    Problem discussion : 0:00
    Approaching the problem : 01:00
    Dry Run Algorithm : 01:36
    Algorithm discussion : 04:33
    Handling Duplicates case : 05:30
    Code explanation : 07:25
    Time Complexity Discussion : 09:29
    Time Complexity : O(n1 + n2 + n3)
    Space Complexity : O(n1 + n2 + n3)
    Problem Link : practice.geeksforgeeks.org/pr...
    C++ Code Link : github.com/Ayu-99/Love-Babbar...
    Please like, share and subscribe if you found the video useful. Feel free to ask in comments section if you have any doubts. :)
    #DataStructuresAndAlgorithms
    #LoveBabbarDSASheet
    #interviewpreparation
    Common elements in 3 sorted arrays solution
    Common elements in 3 sorted arrays Leetcode
    Common elements in 3 sorted arrays C++
    Common elements in 3 sorted arrays C++ Hindi
    Common elements in 3 sorted arrays Hindi
    Checkout the series: 🔥🔥🔥
    👉 Recursion : • Recursion
    👉 Stack and Queue : • Stack And Queue
    👉 Greedy : • Greedy
    👉 Leetcode contests : • Leetcode contests
    👉 Leetcode June Challenge : • Leetcode June Challenge
    👉 Leetcode July Challenge : • Leetcode July Challenge
    LIKE | SHARE | SUBSCRIBE 🔥🔥😊

Komentáře • 50

  • @shubhendupandey3124
    @shubhendupandey3124 Před 3 lety

    As a beginner mujhse ye sheet bilkul nahi solve ho rha tha..par apka video dekh kar bhut help mil jata hai.I always keep on waiting for your video.Plz don't stop. Keep on going. Lots of love and respect 🙏🏻

  • @DecentProgrammer
    @DecentProgrammer Před 2 lety

    Thanks for the video ayushi.

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

    Nice initiative. In this way we will be able to solve all ques of Love Babbar sheet with your help. Keep them coming 🙂

  • @gandhijainamgunvantkumar6783

    Your way of explanation is very good. keep doing it. :) . I am able to write the code by myself when you explain the dry run very clearly.

  • @nirajjain8099
    @nirajjain8099 Před 2 lety

    Wow ma'am great explanation . I have seen all your videos .You are doing great job

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

    Sister many people are already doing this love babbar sheet
    I request you to take a topic and explain your favourite questions on that topic in level wise so that it will help both you and us 🙋‍♂️🙋‍♂️
    Example dynamic programming bahut dikat lagta hei
    Try to do the solution in both top-bottom and bottom to top approaches
    Moreover I have seen your interview sister it was very happy to see you at coding ninjas ☺️☺️keep growing..

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

      Yes Akhil, nice idea. I will solve dp questions from this sheet both top-down and bottom-up approach

  • @harshtiwari8218
    @harshtiwari8218 Před rokem

    Good Explanation👌🤞

  • @mayankdham1609
    @mayankdham1609 Před 3 lety

    Nice work. In this way we are able to solve all ques of sheet😃😃

  • @harshitpatel8618
    @harshitpatel8618 Před 6 měsíci

    class Solution:
    def commonElements(self, A, B, C, n1, n2, n3):
    i, j, k = 0, 0, 0
    ans = []
    prev = float('-inf') # Track previous element to avoid duplicates
    while i < n1 and j < n2 and k < n3:
    if A[i] == B[j] == C[k] and A[i] != prev: # Check for common element and avoid duplicates
    ans.append(A[i])
    prev = A[i]
    elif A[i]

  • @ankitvijay8985
    @ankitvijay8985 Před 2 lety

    Great!!✨

  • @siddhantyadav4531
    @siddhantyadav4531 Před 3 lety

    Great work 👍👍👍👍

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

    Actually while explaining you have taken min of three and incremented the min one, but in the line 40 you are just comparing with the element in first array and element in 2nd array?

  • @abhiram6087
    @abhiram6087 Před 2 lety

    good explanation

  • @ayushsakure6098
    @ayushsakure6098 Před rokem

    @Ayushi Sharma won't the space complexity be O(min(n1, n2, n3)) in place of O(n1+n2+n3) if we are not returning the vector

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

    Hi, while dry running the algorithm you explained using the approach of smallest of 3 and in the code you compared 2 arrays like first starting with smaller of ar1 and ar2 then proceeding...when i used the approach of smallest of 3 in my code is showing output as -1 in some test case..pls help me out with it.My code is as follows:
    class Solution
    {
    public:
    vector commonElements (int ar1[], int ar2[], int ar3[], int n1, int n2, int n3)
    {
    int i = 0, j = 0, k = 0;
    vector ans;
    int prev1, prev2, prev3;
    prev1 = prev2 = prev3 = INT_MIN;
    while (i < n1 && j < n2 && k < n3) {
    while (ar1[i] == prev1 && i < n1)
    i++;
    while (ar2[j] == prev2 && j < n2)
    j++;
    while (ar3[k] == prev3 && k < n3)
    k++;
    if (ar1[i] == ar2[j] && ar2[j] == ar3[k]) {
    ans.push_back(ar1[i]);
    prev1 = ar1[i];
    prev2 = ar2[j];
    prev3 = ar3[k];
    i++;
    j++;
    k++;
    }
    else if (ar1[i] < ar2[j] && ar1[i] < ar3[k]) {
    prev1 = ar1[i];
    i++;
    }
    else if (ar2[j] < ar1[i] && ar2[j] < ar3[k]) {
    prev2 = ar2[j];
    j++;
    }
    else {
    prev3 = ar3[k];
    k++;
    }
    }
    return ans;
    }
    };

    • @tarunkumar.d8379
      @tarunkumar.d8379 Před rokem

      Hi , due to lazy condition check in cpp, while loop mein i

    • @sruthidantuluri415
      @sruthidantuluri415 Před rokem

      @@tarunkumar.d8379 I Didn't get you.. Pls explain clearly..

  • @bikkysharma7253
    @bikkysharma7253 Před 2 lety

    thanks.

  • @ranvijaysinha6595
    @ranvijaysinha6595 Před 2 lety

    Big fan mam

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

    Hello mam, really loving your explanations. I wanted to ask that how do i find the approach of questions. Like now i have learned the approach here but i am not able to find the approach by myself for most of the question.. so do i need to learn the approach of every type of question and then practice??

    • @rehantt7341
      @rehantt7341 Před rokem

      I also have the same question what you did to improve?

  • @tarunkumar.d8379
    @tarunkumar.d8379 Před rokem

    While explaining logic you said we need to increment the index of the lowest element, but in code you are only checking 2 arrays, why and how does it work?

  • @ANUJ-we3su
    @ANUJ-we3su Před 2 lety

    Can u explain what if i becomes out of bound on line 30 we are trying to compare the index ith element of arr1 which ig should throw an error Let us consider this test case (n1=3 n2=4 n3=5 and arr1={3 3 3 } arr2={3 3 4 5} arr3={3 3 5 7 8} )

  • @ecs185_shaileshbharti3

    Hi Dii , Understood the algorithm but in handling the duplicates in else if part ,why are we putting arr1[i] to prev1 while checking arr1[i]

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  Před 2 lety

      Hi, this is because when we get common element, we are updating prev. Why are we having prev is so that we do not add the same common element again and again, so if the current element is same as prev element, it's already considered, so go ahead. (That is what we are doing in while loop). Let me know if you understood it now🙂

    • @ecs185_shaileshbharti3
      @ecs185_shaileshbharti3 Před 2 lety

      @@AyushiSharmaDSA Yaa this I already understood ,What i meant was "we are updating i++ bcz arr1[i]

    • @ecs185_shaileshbharti3
      @ecs185_shaileshbharti3 Před 2 lety

      @@AyushiSharmaDSA I will dry run the program few more times ,i guess i am missing or mismatching something 😅

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

    i didn't got the point of time complexity like there is a while loop inside of while i know it is n1+n2+n3 but in my mind it's o(n^2) can u please explain that please!!

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

      it is because each element is visited only once

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

      @@AyushiSharmaDSA ohhh yuppp now i got the point thanks a lot for explaining

  • @Oikawa7
    @Oikawa7 Před 2 lety

    why the time complexity is not O(n^2) (here we are using the while loop inside the while loop) can you explain this?

    • @tarunkumar.d8379
      @tarunkumar.d8379 Před rokem

      You are visiting every element only once

    • @Oikawa7
      @Oikawa7 Před rokem

      @@tarunkumar.d8379 thank you
      😃😃

  • @EternalCoders
    @EternalCoders Před 3 lety

    Maximum xorr value vala program bata do

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

    Mam plz Python code bhi dijiye bhut se log Python se dsa krte hai to mam pzz add Python code also

  • @rohitvhanmore8742
    @rohitvhanmore8742 Před 6 měsíci

    Nhi samjha kuch prev wala concept

  • @harshita886
    @harshita886 Před rokem

    😡😡