Find All Duplicates in an Array | Clean | Easy | Leetcode 442 | codestorywithMIK

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • iPad PDF Notes - github.com/MAZHARMIK/Intervie...
    Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 87th Video of our Playlist "Arrays 1D/2D : Popular Interview Problems".
    In this video we will try to solve an extremely good problem :
    Find All Duplicates in an Array | Clean | Easy | Leetcode 442 | 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 : Find All Duplicates in an Array | Clean | Easy | Leetcode 442 | codestorywithMIK
    Company Tags : Pocket Gems, Amazon, Meta
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/find-al...
    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
    Approach Summary :
    The given approach is designed to find duplicates in an array of integers efficiently. It works by utilizing the properties of the array itself to mark visited elements.
    Here's how the approach works:
    1. Iterate through each element of the input array `nums`.
    2. For each element `num`, take its absolute value (`abs(nums[i])`) to ensure positive index access.
    3. Check if the element at index `num - 1` (assuming 1-based indexing) is negative. If it is negative, it means `num` has been encountered before, so it's a duplicate. In this case, add `num` to the result vector.
    4. If the element at index `num - 1` is not negative, mark it as visited by multiplying it by -1 (`nums[num - 1] *= -1`). This indicates that `num` has been encountered.
    5. Repeat steps 3-4 for all elements in the array.
    6. Return the result vector containing duplicates.
    This approach effectively utilizes the sign of the elements in the input array to mark visited elements, making it a space-efficient solution with a time complexity of O(n), where n is the size of the input array.
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    ✨ Timelines✨
    00:00 - Introduction
    01:24 - Brute Force 1 and Brute Force 2
    02:43 - Optimal Approach
    10:33 - Coding it 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 #newyear2024

Komentáře • 56

  • @codestorywithMIK
    @codestorywithMIK  Před 4 měsíci +15

    Happy Holi everyone ❤❤
    Enjoy and spend time with your family. 🎉🎉❤️❤️
    Similar Pattern Qn - Leetcode-41 (Hard version of this pattern) - czcams.com/video/lyjOwaUEWWQ/video.htmlsi=ClWuoKanT2plI8zi

  • @thegreekgoat98
    @thegreekgoat98 Před 4 měsíci +4

    What a legend yaar. Pity of those who dont know about this channel. Absolute gem

  • @KrishnaGupta-kd6jh
    @KrishnaGupta-kd6jh Před 4 měsíci +3

    The way you teach is absolutely great, sir. Please do not stop uploading these series ....

  • @shraban8508
    @shraban8508 Před 4 měsíci +1

    Thank u sir for explaining in such a elegant manner. Your channel is my one stop solution for leetcode probs understanding.

  • @MohitKumar-yx9hr
    @MohitKumar-yx9hr Před 4 měsíci +1

    thank you for giving such kind of different approach to solve the problem in simple way 🙏❤💯

  • @oqant0424
    @oqant0424 Před 4 měsíci +1

    Last approach was lit 🔥🔥
    Thanks ❤❤
    POTD DONE [25.3.24]✅✅

  • @iWontFakeIt
    @iWontFakeIt Před 4 měsíci +1

    swap sort se solve kiya but ye approach bhi acha ha socha tha maine iske bare mein

  • @AnjuGupta-sz1uk
    @AnjuGupta-sz1uk Před 4 měsíci +1

    Learnt one new concept ... Thanku so much Bhaiya

  • @abcd76820
    @abcd76820 Před 4 měsíci +1

    I solved it coz i learnt this method from you in some other vdeos .....

  • @KINGSLAYERAYT
    @KINGSLAYERAYT Před 4 měsíci +2

    This question can be solved easily using cyclic sort

  • @user-it8wq6qg4p
    @user-it8wq6qg4p Před 4 měsíci +1

    Great work

  • @retromonk88
    @retromonk88 Před 4 měsíci +1

    i was struggling when value at any index becomes equal to size of array, thanks i got what i was doing wrong

  • @onelove177
    @onelove177 Před 4 měsíci +2

    Simply apply cycling sort later run a loop which is not at their correct index that will be ans

  • @aditimahabole1761
    @aditimahabole1761 Před 4 měsíci +1

    think of it as find cycle in link list

  • @rashiarora4778
    @rashiarora4778 Před 4 měsíci

    for sorting approach why the time complexity is nlogn? and why not n+log(n) ??
    also why did we push n and not nums[idx]*(-1)?

  • @nikhil_kolla_12
    @nikhil_kolla_12 Před 4 měsíci +1

    Good video, please check the description, the "Approach Summary" is not for this question.

  • @jeethora586
    @jeethora586 Před 4 měsíci

    Happy holi sir

  • @riderprovider44
    @riderprovider44 Před 4 měsíci +2

    happy holi sir

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci

      A very Happy Holi brother ❤️❤️🙏🙏

    • @abhishek_singh512
      @abhishek_singh512 Před 4 měsíci

      @@codestorywithMIK sir your voice are little low in this video .. 😥😥😥😥

  • @naveenkumarkota2090
    @naveenkumarkota2090 Před 4 měsíci +1

    mike we came up with 0(n) and 0(n) complexities will the interviewer be satisfied ? or he assume with those constraints its so easy?

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci

      That’s fine.
      But it’s highly likely they will ask for optimised version if they have given you the constraints.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 4 měsíci

    good explanation please update link.🎉❤

  • @joydeep-halder
    @joydeep-halder Před 4 měsíci

    Happy Holi ❤❤

  • @abhijitroy1958
    @abhijitroy1958 Před 4 měsíci

    class Solution {
    public:
    vector findDuplicates(vector& nums) {
    int maxi=INT_MIN;
    for(auto it:nums){
    maxi=max(maxi,it);
    }
    vectorv(maxi+1,0);
    vectorv1;
    for(int i=0;i

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci

      Actually this is not a constant space. The maximum value can be anything. So the size of array depends on maximum value.

    • @abhijitroy1958
      @abhijitroy1958 Před 4 měsíci

      ​@@codestorywithMIK thanks for confirming

  • @RohanSingh-dj6kb
    @RohanSingh-dj6kb Před 4 měsíci

    Can anyone list questions following this pattern ?

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci

      Leetcode-41 (Hard version of this pattern)
      Surprisingly it’s Today’s POTD - czcams.com/video/lyjOwaUEWWQ/video.htmlsi=ClWuoKanT2plI8zi

  • @abhisheknishad2282
    @abhisheknishad2282 Před 4 měsíci

    brother . I have already done this but having a doubt is that why vector is not taking space in this ? isn't it O(n) space?
    help me out

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci +1

      Actually we usually don’t consider the result data structure(which we have to return) in the space complexity.

    • @abhisheknishad2282
      @abhisheknishad2282 Před 4 měsíci

      Ohh.. Thanks

  • @naikajsevak6090
    @naikajsevak6090 Před 4 měsíci

    bhaiya are u in which company right now???

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci +1

      Know everything about me here - github.com/MAZHARMIK ❤️😇

  • @manas_thakre
    @manas_thakre Před 4 měsíci

    i want the pdf please

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci

      iPad PDF Notes (Added in Description also) github.com/MAZHARMIK/Interview_DS_Algo/blob/master/iPad%20PDF%20Notes/Leetcode-442-Find%20All%20Duplicates%20in%20an%20Array.pdf

    • @manas_thakre
      @manas_thakre Před 4 měsíci

      Thanks!!

  • @iiERIC
    @iiERIC Před 4 měsíci

    New Vector Liya To Space Complexity O(n) Ho gyi Na....?

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci

      Which Approach Faiz ?

    • @advaithmahendrakar
      @advaithmahendrakar Před 4 měsíci

      But question me Array return karne ke liye bola hai

    • @iiERIC
      @iiERIC Před 4 měsíci

      @@codestorywithMIK Optimal 🎯

    • @codestorywithMIK
      @codestorywithMIK  Před 4 měsíci +2

      Actually we usually don’t consider the result data structure(which we have to return) in the space complexity.

    • @iiERIC
      @iiERIC Před 4 měsíci

      @@codestorywithMIK Got it, Thanks 👍🏼

  • @sjxsubham...
    @sjxsubham... Před 4 měsíci +1

    sort(nums.begin(), nums.end());
    vector ans;
    for(int i = 0; i< nums.size()-1; i++){
    if(nums[i] == nums[i+1]{
    ans.push_back(nums[i]);
    }
    }
    return ans;
    Easy solution 😄

    • @kartikforwork
      @kartikforwork Před 4 měsíci

      but sorting o(nlongn) lege, aur tujhe ques me mention kia hai o(n) and o(1) me karne ke lia, interview se nikal dege aaise karega to

    • @kartikforwork
      @kartikforwork Před 4 měsíci

      remove which line, sort? nope without sorting ur code won't work@@sjxsubham...

    • @sjxsubham...
      @sjxsubham... Před 4 měsíci

      @@kartikforwork baat to shi hai