Intersection of Two Arrays | 4 Approaches | Leetcode 349
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
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
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Arrays/Leetcode%20Easy/Intersection%20of%20Two%20Arrays.cpp
Leetcode Link : https://leetcode.com/problems/intersection-of-two-arrays
My DP Concepts Playlist : https://youtu.be/7eLMOE1jnls
My Graph Concepts Playlist : https://youtu.be/5JGiZnr6B5w
My Recursion Concepts Playlist : https://www.youtube.com/watch?v=pfb1Zduesi8&list=PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Subscribe to my channel : https://www.youtube.com/@codestorywithMIK
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
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