Common elements in 3 sorted arrays | Array | Love Babbar DSA Sheet | Hindi
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 🔥🔥😊
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 🙏🏻
Thank you Shubhendu. This gives me motivation :)
Thanks for the video ayushi.
Nice initiative. In this way we will be able to solve all ques of Love Babbar sheet with your help. Keep them coming 🙂
Yes I will 😃😃
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.
thank you so much 😄
Wow ma'am great explanation . I have seen all your videos .You are doing great job
Thank you so much Niraj😊
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..
Yes Akhil, nice idea. I will solve dp questions from this sheet both top-down and bottom-up approach
Good Explanation👌🤞
Nice work. In this way we are able to solve all ques of sheet😃😃
Thank you :)
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]
Great!!✨
Thank you :)
Great work 👍👍👍👍
Thank you:)
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?
good explanation
Thank you :)
@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
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;
}
};
Hi , due to lazy condition check in cpp, while loop mein i
@@tarunkumar.d8379 I Didn't get you.. Pls explain clearly..
thanks.
Welcome :)
Big fan mam
Thank you Ranvijay. Blessed 😊
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??
I also have the same question what you did to improve?
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?
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} )
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]
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🙂
@@AyushiSharmaDSA Yaa this I already understood ,What i meant was "we are updating i++ bcz arr1[i]
@@AyushiSharmaDSA I will dry run the program few more times ,i guess i am missing or mismatching something 😅
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!!
it is because each element is visited only once
@@AyushiSharmaDSA ohhh yuppp now i got the point thanks a lot for explaining
why the time complexity is not O(n^2) (here we are using the while loop inside the while loop) can you explain this?
You are visiting every element only once
@@tarunkumar.d8379 thank you
😃😃
Maximum xorr value vala program bata do
Mam plz Python code bhi dijiye bhut se log Python se dsa krte hai to mam pzz add Python code also
Okay will add python code 👍🏻
Nhi samjha kuch prev wala concept
😡😡