Intersection of Two Arrays II | 2 Approaches | Easy Explanations | Leetcode 350 | codestorywithMIK
Вставка
- Опубліковано 30 чер 2024
- Whatsapp Community Link : www.whatsapp.com/channel/0029...
This is the 45th Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a good practice problem : Intersection of Two Arrays II | 2 Approaches | Easy Explanations | Leetcode 350 | codestorywithMIK
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.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Intersection of Two Arrays II | 2 Approaches | Easy Explanations | Leetcode 350 | codestorywithMIK
Company Tags : META
My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/interse...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach 1: Using Hash Map
Time Complexity: O(n) Space Complexity: O(n)
Hash Map for Counting Elements:
Traverse the first array (nums1) and use an unordered_map (or HashMap in Java) to count the occurrences of each element.
Intersection Calculation:
Traverse the second array (nums2).
For each element in nums2, check if it exists in the map with a count greater than zero.
If it exists, add it to the result list and decrement its count in the map.
Advantages:
Efficient in terms of both time and space for large arrays with many duplicate elements.
Approach 2: Using Sorting and Two Pointers
Time Complexity: O(n log n) Space Complexity: O(n)
Sort Both Arrays:
Sort both input arrays (nums1 and nums2).
Two-Pointer Technique:
Use two pointers to traverse the sorted arrays.
If elements at both pointers are equal, add the element to the result list and increment both pointers.
If the element in nums1 is smaller, increment the pointer for nums1.
If the element in nums2 is smaller, increment the pointer for nums2.
Advantages:
Simple and easy to understand.
Efficient when the arrays are already sorted or nearly sorted.
Comparison
Efficiency: The hash map approach has a linear time complexity, making it faster for large arrays. The sorting and two-pointer approach has a higher time complexity due to the sorting step.
Space: Both approaches use extra space, but the hash map approach uses additional space proportional to the number of unique elements in the first array, whereas the sorting approach uses extra space mainly for the sorted arrays.
Use Case: The hash map approach is more efficient for unsorted arrays with many duplicates, while the sorting and two-pointer approach is better for arrays that are already sorted or when space is a concern.
✨ Timelines✨
00:00 - Introduction
#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 #newyear2024
Bro Isi time harroj upload kiya kro kuch log morning me solve krte hai
Third Approach Using Binary Search:
Bhaiya mujhse pahle 1 approach hit Kiya tha jab mey ye question padha to and then 2 approach 😊
3rd Approach:- Using Binary Search 🔎 in nums2
bhaiyya partion dp videos..BTW thank you bhaiyya
Thanks a lot bhaiya ❤❤
This question has already been solved in "Leetcode Easy Playlist" of this channel.
thanks!!
bhaiya ek chhota sa video aapke DSA wale reposetry pe bna dijiye please ki usko kaise efficiently use karna hai ,uske Arrays wale folder mein bht sara problems hai unke subtopics nhi samjh aa rha
pls take follow up questions also given below the questions
I have submitted count sort method my code is this.thank for your previous video for countsort.
If sort using merge sort then space will be O(n)
What should be the answers to the follow up questions??
Mene bhi first sort karke two pointer apply kiya tha then socha map se bhi ho sakta hai..
leetcode 1267. Count Servers that Communicate
sir i have a question sort algorithm is taking space but why did you say that space is o(1)??
bhai apki leetcode ki profile ka link dena
Constraint <= 1000 the
Bhaiya ek baar follow up question ka answer bhi pin kar do please!!