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

Поделиться
HTML-код
  • Опубликовано: 8 фев 2025

Комментарии • 61

  • @Ryanamahaffey
    @Ryanamahaffey 3 года назад +42

    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!

    • @MyPodie
      @MyPodie 5 месяцев назад

      Exactly, took me a while to really get what the explanation means.

  • @meowmaple
    @meowmaple 3 года назад +18

    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!

  • @BROOKnim
    @BROOKnim 2 года назад +4

    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.

  • @hussamq9909
    @hussamq9909 3 года назад +6

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

  • @nikhilchauhan5837
    @nikhilchauhan5837 3 года назад +16

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

    • @badrirao6472
      @badrirao6472 2 месяца назад

      How did u solve that question ?

  • @leetcodeespanol59
    @leetcodeespanol59 3 года назад +2

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

  • @boris---
    @boris--- Год назад +58

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

    • @huckleberryfinn8795
      @huckleberryfinn8795 10 месяцев назад +2

      Its easy for a senior software engineer 😂

    • @focminging652
      @focminging652 6 месяцев назад

      @@huckleberryfinn8795 lol

    • @dabrahams4760
      @dabrahams4760 6 месяцев назад +1

      You taking applications?

    • @MaksymOliinyk-z5u
      @MaksymOliinyk-z5u 4 месяца назад

      ​@@huckleberryfinn8795 im pretty sure its not if he is not solving leetcode questions regularly :)

  • @OmarChida
    @OmarChida 3 года назад +3

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

  • @styles441
    @styles441 2 месяца назад +1

    Best approach

  • @andreytamelo1183
    @andreytamelo1183 2 года назад

    Thanks!

    • @NeetCode
      @NeetCode  2 года назад

      Thank you so much again!!!

  • @setiyakitchen
    @setiyakitchen 5 месяцев назад +1

    class Solution {
    public List findDisappearedNumbers(int[] nums) {
    List list = new ArrayList();
    int n = nums.length;
    for(int i=0;i

  • @danielcustduran
    @danielcustduran 3 года назад

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

  • @amanrai5285
    @amanrai5285 2 года назад +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.

  • @venkatasundararaman
    @venkatasundararaman 3 года назад +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 2 года назад

      Great one

    • @RituSharma-zp7xs
      @RituSharma-zp7xs 2 года назад +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 Год назад

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

    • @sadekjn
      @sadekjn Год назад +1

      @@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 Год назад +1

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

  • @greentea_vsrg
    @greentea_vsrg 2 года назад +1

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

  • @techsavy5669
    @techsavy5669 2 года назад +1

    Do it with cyclic sort, will be less confusing✌

  • @АлександрАлександр-о4к2ъ

    Amassing solution!!!

  • @ngneerin
    @ngneerin 3 года назад

    Keep up the leetcode!

  • @sohelsayyad5572
    @sohelsayyad5572 5 месяцев назад

    DON'T READ IF YOU have ALREADY UNDERSTOOD
    if you are confused/not clear :
    # you need to have a list of n integers then only it works, that's case scenario.
    In the example array/list have DUPLICATES .... .you need to have duplicates in the array and your task is to find what numbers are missing, whose position these DUPLICATES have taken. ....
    if you try array [2,3,7] array it will give out of range issue. because MAXIMUM number in array should be

  • @vdyb745
    @vdyb745 2 года назад

    Superb !!!

  • @amrholo4445
    @amrholo4445 2 года назад

    Thanks a lot sir

  • @samuelcordova5183
    @samuelcordova5183 3 года назад

    please do word pattern II. love your videos

  • @faisalabdulkadir9439
    @faisalabdulkadir9439 3 года назад

    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

  • @marekglowacki2607
    @marekglowacki2607 2 года назад

    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]

  • @meghanagowda9967
    @meghanagowda9967 5 месяцев назад

    Summary:
    Iterating through the array and marking the value at visited index negative.
    at the end the values which are not marked negative and its corresponding indicies are the values missing in the array.

  • @pranshusharma26
    @pranshusharma26 3 года назад +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 3 года назад

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

    • @serhattural3168
      @serhattural3168 3 года назад

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

    • @Ryanamahaffey
      @Ryanamahaffey 3 года назад

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

  • @shivansh.speaks
    @shivansh.speaks Год назад +1

    goodquestion

  • @balajipattabhiraman
    @balajipattabhiraman 3 года назад

    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

  • @shubhk33
    @shubhk33 3 года назад

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

    • @rafay_syed
      @rafay_syed 3 года назад

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

    • @leeroymlg4692
      @leeroymlg4692 2 года назад

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

  • @Ericnguyencsca
    @Ericnguyencsca 9 месяцев назад

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

  • @naimurhasanrwd
    @naimurhasanrwd Месяц назад

    Awesome

  • @SoyAudio
    @SoyAudio 3 года назад

    I love you!!!!!!!!

  • @rukaiyakhan2406
    @rukaiyakhan2406 2 года назад +1

    can you please name this algorithm

  • @cgqqqq
    @cgqqqq 3 года назад

    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...

  • @jayeshkaushik2975
    @jayeshkaushik2975 3 года назад

    hey, can you please explain the leetcode contest problems?

  • @Historyiswatching
    @Historyiswatching 2 года назад

    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 2 года назад

      look up absolute operation

  • @janekmuric
    @janekmuric 3 года назад

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

  • @VarunMittal-viralmutant
    @VarunMittal-viralmutant 3 года назад

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

    • @ShaolimMatadorPorco69
      @ShaolimMatadorPorco69 3 года назад +2

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

    • @VarunMittal-viralmutant
      @VarunMittal-viralmutant 3 года назад

      @@ShaolimMatadorPorco69 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)

  • @4r4zzz
    @4r4zzz Год назад

    fucking hell, this is awesome

  • @jyothibavandla1251
    @jyothibavandla1251 3 месяца назад

    Hi

  • @mokoboko2482
    @mokoboko2482 Год назад

    ok that's fucking cool