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

preview_player
Показать описание
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)


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: 🔥🔥🔥

LIKE | SHARE | SUBSCRIBE 🔥🔥😊
Рекомендации по теме
Комментарии
Автор

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] <= B[j] and A[i] <= C[k]:
i += 1
elif B[j] <= A[i] and B[j] <= C[k]:
j += 1
else:
k += 1

return ans

harshitpatel
Автор

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

nirajjain
Автор

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

sparshpandey
Автор

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.

gandhijainamgunvantkumar
Автор

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

ravulaakhilsurya
Автор

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

mayankdham
Автор

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 🙏🏻

shubhendupandey
Автор

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 <int> commonElements (int ar1[], int ar2[], int ar3[], int n1, int n2, int n3)
{
int i = 0, j = 0, k = 0;
vector<int> 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;
}


};

devdalia
Автор

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

sapnajha
Автор

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?

yourentertainer
Автор

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?

tarunkumar.d
Автор

@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

ayushsakure
Автор

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

vaidanshkukreja
Автор

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

ANUJ-wesu
Автор

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

Oikawa
Автор

Maximum xorr value vala program bata do

EternalCoders
Автор

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

shubhasheeshkundu
Автор

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] <arr2[j] ?...
i got how prev will work and i++ will move to the next element in that array but still having confusion in this...

ecs_shaileshbharti