Find All Numbers Disappeared in an Array - Leetcode 448 - Python

Sdílet
Vložit
  • čas přidán 2. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    💡 BINARY SEARCH PLAYLIST: • Binary Search
    📚 STACK PLAYLIST: • Stack Problems
    Problem Link: leetcode.com/problems/find-al...
    Python Code: github.com/neetcode-gh/leetco...
    0:00 - Read the problem
    0:49 - Drawing Explanation
    6:38 - Coding Explanation
    leetcode 448
    This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
    #microsoft #interview #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 50

  • @Ryanamahaffey
    @Ryanamahaffey Před 2 lety +37

    This one was a bit hard to grasp at first until I saw the code!
    Great video regardless, but I think a better test case to explain would have been [1, 4, 4, 2].
    Seeing you go back and mark idx 1 (for the number 2) as a negative 4 would have made things much clearer for me!

  • @meowmaple
    @meowmaple Před 2 lety +16

    Previously had done this question but not in the O(1) space complexity way, and wow I must say that this is such an interesting way of making it O(1) space complexity!

  • @boris---
    @boris--- Před rokem +20

    Yeah I can go back to McDonald if this is easy question....

  • @nikhilchauhan5837
    @nikhilchauhan5837 Před 2 lety +13

    Constant space problems are always tricky i got this one in interview 😔. Can you make more videos on solving constant space problems

  • @leetcodeespanol59
    @leetcodeespanol59 Před 2 lety +2

    Thanks for all your hard work Neetcode! Its great to see you consistently upload videos for us! Keep it up!

  • @hussamq9909
    @hussamq9909 Před 2 lety +5

    Great explanation, you never disappoint. Keep up the good work.

  • @BROOKnim
    @BROOKnim Před rokem +2

    if some people dont understand this very clearly, there is this sorting technique called cyclic sort ( sorts array in O(n) if elements are in range of [1,n] ). look it up , will be useful.

  • @danielcustduran
    @danielcustduran Před 2 lety

    I never figured out this approach. I solved it using sets but this doesn't meet the constraint of space complexity. Great!!!

  • @ngneerin
    @ngneerin Před 2 lety

    Keep up the leetcode!

  • @OmarChida
    @OmarChida Před 2 lety +1

    I immediately though of cyclic sort while reading this problem. Turns out it is the correct solution.

  • @user-vy7gk2qn9l
    @user-vy7gk2qn9l Před 2 lety

    Amassing solution!!!

  • @amrholo4445
    @amrholo4445 Před 2 lety

    Thanks a lot sir

  • @faisalabdulkadir9439
    @faisalabdulkadir9439 Před 2 lety

    I really respect u and all the work u do , u have really helped me a lot , pls can the next problem I solve be the increasing in order search tree leetcode 897, I’m finding it hard to grasp the recursion process

  • @samuelcordova5183
    @samuelcordova5183 Před 2 lety

    please do word pattern II. love your videos

  • @vdyb745
    @vdyb745 Před 2 lety

    Superb !!!

  • @amanrai5285
    @amanrai5285 Před rokem +1

    I had a hard time to understand the problem:
    My way of understanding is:
    We are using the indexes to our advantage as they are in range of n. So we are marking the values at the indexes as -ve to signify(the number in the array) is marked visited by putting a -ve sign on the particular index. Finally we loop through all the elements and find which indexes are not -ve and then return them.

  • @balajipattabhiraman
    @balajipattabhiraman Před 2 lety

    Could we sort and see if the index value and the actual value align and if not detect missing value and update an offset on what the next index would contain if there is no next missing value

  • @venkatasundararaman
    @venkatasundararaman Před 2 lety +6

    Instead of changing them to negative, we can just move the numbers to their index and then finally run a loop to check if the number value at an index is index+1 if not then add that to our result.

    • @shaikfaiz593
      @shaikfaiz593 Před 2 lety

      Great one

    • @RituSharma-zp7xs
      @RituSharma-zp7xs Před 2 lety +13

      if you move them to their respective index, you will overwrite some of the numbers which we will visit in the future. This could lead to an incorrect response.

    • @frida8519
      @frida8519 Před rokem

      @@RituSharma-zp7xs you would be swapping numbers, not necessarily overwriting them

    • @sk_4142
      @sk_4142 Před rokem

      @@frida8519 If you swap the numbers, that still results in errors because you might skip the number that you swap. You could probably handle that, but you'll end up realizing that it gets messy very quickly compared to Mr NeetCode's solution.

    • @frida8519
      @frida8519 Před rokem

      @@sk_4142 yea you're right. I tried doing it the other way and it was just all over the place lol.

  • @techsavy5669
    @techsavy5669 Před rokem

    Do it with cyclic sort, will be less confusing✌

  • @greentea_vsrg
    @greentea_vsrg Před rokem +1

    I feel like doing this in constant space makes it a medium

  • @jayeshkaushik2975
    @jayeshkaushik2975 Před 2 lety

    hey, can you please explain the leetcode contest problems?

  • @marekglowacki2607
    @marekglowacki2607 Před rokem

    I've devised following solution, but I'm not sure if it still counts as O(1) memory:
    class Solution:
    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
    nums = [0] + nums
    for n in nums:
    if nums[abs(n)] > 0:
    nums[abs(n)] *= -1

    return [i for i,v in enumerate(nums) if v > 0]

  • @andreytamelo1183
    @andreytamelo1183 Před 2 lety

    Thanks!

    • @NeetCode
      @NeetCode  Před 2 lety

      Thank you so much again!!!

  • @theresilientpianist7114
    @theresilientpianist7114 Před 10 měsíci +1

    goodquestion

  • @SoyAudio
    @SoyAudio Před 2 lety

    I love you!!!!!!!!

  • @cgqqqq
    @cgqqqq Před 2 lety

    does this algo have a name? I saw a couple of problems was solved by the same method but none mentioned the name of the method...

  • @pranshusharma26
    @pranshusharma26 Před 2 lety +1

    Can we solve it like, first take the minimum and maximum value present in the given list.Traverse between minimum to maximum value and then return those numbers which are not in given list ???

    • @MarsTheProgrammer
      @MarsTheProgrammer Před 2 lety

      Yes but traversing numbers between min and max will not result in O(n) time.

    • @serhattural3168
      @serhattural3168 Před 2 lety

      If you sort the array first, it works with time complexity O(nlogn)

    • @Ryanamahaffey
      @Ryanamahaffey Před 2 lety

      @@serhattural3168 nlogn is still worse than just O(n) a with constant space as shown in the video explanation though

  • @rukaiyakhan2406
    @rukaiyakhan2406 Před rokem +1

    can you please name this algorithm

  • @shubhk33
    @shubhk33 Před 2 lety

    is return list(set(range(1,len(nums)+1)) - set(nums)) a better solution?

    • @rafay_syed
      @rafay_syed Před 2 lety

      That's what I did and I got a higher runtime

    • @leeroymlg4692
      @leeroymlg4692 Před rokem

      it's a good solution but it is not 0(1) memory as you are creating an additional set

  • @lilyh4573
    @lilyh4573 Před 2 lety

    How come when you multiply any positive number with a negative number (like -1) it turns negative? I've been told that's how it works in school.... but no one told me why?!

    • @rukna3775
      @rukna3775 Před 2 lety

      look up absolute operation

  • @thangnguyenduc9969
    @thangnguyenduc9969 Před měsícem

    i don't even know how can I come up with this optimal solution in an interview.

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

    fucking hell, this is awesome

  • @janekmuric
    @janekmuric Před 2 lety

    That's not O(1) space, it's still O(1)

  • @VarunMittal-viralmutant

    A two liner:
    valid_set = set(range(1, len(nums) + 1))
    return (valid_set - valid_set.intersection(nums))

    • @ArquimedesOfficial
      @ArquimedesOfficial Před 2 lety +2

      Can u tell us about Time and Space Complexity for this one?...

    • @VarunMittal-viralmutant
      @VarunMittal-viralmutant Před 2 lety

      @@ArquimedesOfficial Time complexity O(n) and Space complexity O(n) due to the extra valid_set. To elaborate:
      Creating a set of entire range - O(n) Time complexity
      Finding intersection - O(min(len(s1), len(s2)) which could be O(n) in worst case
      Difference - O(n)
      Total time complexity: O(n) + O(n) + O(n) = O(n)

  • @dumbfailurekms
    @dumbfailurekms Před rokem

    ok that's fucking cool