Intersection of Two Arrays | 4 Approaches | Leetcode 349

preview_player
Показать описание
This is the 27th Video of our Playlist "Leetcode Easy : Popular Interview Problems".
In this video we will try to solve a very good practice problem : Intersection of Two Arrays | 4 Approaches | Leetcode 349

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.

Problem Name : Intersection of Two Arrays | 4 Approaches | Leetcode 349
Company Tags : Microsoft, Amazon

Approach Summary :
Approach-1 (Using two sets):
- Time Complexity (T.C): O(m+n)
- Space Complexity (S.C): O(m+n)
- Two unordered sets are used to store unique elements from each input vector.
- The second set is then populated with common elements found while iterating through the second vector.
- The final result is obtained by converting the second set to a vector.

Approach-2 (Using one set):
- T.C: O(m+n)
- S.C: O(m+n)
- Utilizes only one unordered set to store unique elements from the first vector.
- Common elements are identified and added to the result vector while iterating through the second vector.

Approach-3 (Sorting and binary search):
- T.C: O(n + mlogm)
- S.C: O(n)
- Sorts the first vector and performs binary search on it for each element in the second vector.
- Common elements are stored in a set, and the final result is obtained by converting the set to a vector.

Approach-4 (Sorting and two-pointers):
- T.C: O(n + m)
- S.C: O(1)
- Sorts both vectors and uses two pointers to iterate through them.
- Common elements are added to the result vector, and duplicates are handled by advancing the pointers.
- This approach has better space complexity as it uses constant space for the result vector.

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

✨ Timelines✨
00:00 - Introduction
01:06 - Approach-1
04:22 - Approach-2
06:44 - Approach-3
09-25 - Approach-4
14:11 - Coding all up

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #2024 #newyear
Рекомендации по теме
Комментарии
Автор

Watching Bhaiya's channel grow from 3k to 30k subs is incredible—both his channel's growth and your knowledge surge have experienced a remarkable 10x boost. As you aim for a million subs, may your journey continue to multiply with success! 🌟

dhairyachauhan
Автор

Because of you sir I have 64 days consistentency in leetcode in 2024

aadarshpandit
Автор

POTD DONE
(10.3.24) ✅✅

Solved using two approaches on my own

got two learnings from u
Thanks ❤❤

oqant
Автор

Many congratulations bhaiyaa🎉..keep growing..your stories are just too good.. leetcode became easy because of you🥺

shreyaverma
Автор

Thanks a lot bhaiya question easy hota hai fir v kuch naya sikha hi dete ho aap ❤❤

gauravbanerjee
Автор

class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
vector<int> ans;
unordered_set<int> st;
int i = 0, j = 0;
while(i < nums1.size() && j < nums2.size()){
if(nums1[i] < nums2[j]) i++;
else if(nums1[i] > nums2[j]) j++;
else{
if(st.find(nums1[i]) == st.end()){
st.insert(nums1[i]);
ans.push_back(nums1[i]);
}
i++;
j++;
}
}
return ans;
}
};❤❤

utkarshsahay
Автор

Thank you sir. For discussing all approaches with code ❤

aadarshpandit
Автор

Thanks a lot for multiple approaches. Your efforts are clearly seen in videos.
Congratulations on 31k subs

DevOpskagyaan
Автор

Thank you so much sir for all the approaches ❤
One thing sir
When i was approaching other's code on LeetCode discussion I did encounter a solution consisting of the same 2 pointer approach but for removing the duplication the algorithm used unique and erase function. Sir if possible please provide a dry run how this two functions work.❤

ektuhaso
Автор

please came up with "Remove Outermost Parentheses" solution with several approaches. Eagerly waiting.

sayantanpoddar
Автор

Please complete the recursion series sir❤

RanjeetKumar-tlnd
Автор

thanks for the explanation but,
can we solve it without using Hash set ??and by just using Arrays?

bharatjagoar
Автор

:) Python


class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:

intersection =
return list(intersection)

ArjunSaxena-wlqs
Автор

I want one suggestion is watching your videos solutions and doing worth it or should I try of my own

swagboltey
Автор

Sir please start explaining leetcode contest questions also

akashdeepgupta
Автор

hi, can you please explain 4th question of todays lc contest?

pranavarora
Автор

I am very new to dsa please help me what to watch and start with dsa concepts +practical can u please rearrange videos for beginners

kaifshaikh
Автор

Bhaiya i have some doubt how can i discuss with u personall?

yashvalecha
Автор

sir isme runtime error kaise aa rha haii??

set<int>s;
sort(nums2.begin(), nums2.end());
for(auto it: nums1){
auto x = lower_bound(nums2.begin(), nums2.end(), it)-nums2.begin();;



}
vector<int>ans;
for(auto it:s)ans.emplace_back(it);
return ans;

Algorithmswithsubham
Автор

Bhaiya abhi DSA start nahi Kiya abhi Web development seekh Raha hoon (JavaScript). Kya mujhe DSA JavaScript se start karna chahiye ya aap bata do ❤

akarsh_
welcome to shbcf.ru