@supercarpro

Friggin calculus taught me the sum is just n(n+1)/2 so just loop through array and keep subtracting from that, remaining will be result. Tried using that formula in an interview once and bro was just like "but thats just math" and ignored it lmao. Thanks for the vids, really high quality and I get excited seeing you covered a problem I need help on.

@robogirlTinker

(Python - XOR)
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        res = len(nums)
        for i in range(len(nums)):
            res ^= i ^ nums[i]
        return res

@musicgotmelike9668

Really appreciate the fact that you explain multiple solutions. Always interesting to learn them!

@lee_land_y69

you can also use a closed for of sequence (1 + 2 + ...+ n)   and subtract a sum of nums. solution would be len(nums)*(len(nums) + 1) / 2 - sum(nums)

@BigFishMachine

because of you i am feeling addicted to these problems, now i am sure with some time and practice i will get in Big Tech

@Huytn-12

Thanks!

@sealwithawkwardness3951

I'm glad that math finally kicked in for me, remembering Gauss's formula for sums.

@jose.ambrosio

Your videos are helping me a lot. Thanks, Neetcode!

I implemented a simpler solution for this problem with the same time and memory complexity.

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        sumAll = 0
        
        for i in range(len(nums) + 1):
            sumAll += i

        return sumAll - sum(nums)

@codeisawesome369

Thanks for the upload! I grokked the XOR explanation much better thanks to this as the Leetcode editorial was incomprehensible. Feedback: would've been a bit nicer if the explanation of why 5^3^5 would yield 3 - even though the order is not 5^5^3. I was able to work it out on paper, but the video would be more complete that way 🙂

@juliagulia7384

The way you coded it is so much cleaner, thank you! I was using my handy gauss summation formula but adding and subtracting seems way easier

@VibeBlind

result = len(nums)        
for i, n in enumerate(nums):
    result += i-n
return result

@theedmaster7748

Holy shit, I gasped when you mentioned that you just XOR elements in the entire range with elements in nums. 

I knew that XOR was used, but I was struggling with how to deal with the lack of order in nums

So smart!

@skyplanet9858

Thanks for the video. A more general form that can handle any array is this
"def missingNumber(array):
    res_all=0
    for i in range(min(array),max(array)):
 
        res_all+=i-array[i]
    return res_all+max(array)"

@billykotsos4642

I had no idea XOR was an operator in python !  WOW !

@triscuit5103

The clearest explanation I've seen on this. Hot stuff. Thanks for your videos, they are the bomb.

@vishalsinghsengar7951

I was asked the same question in interview, with a twist that more than 1 number can be missing, with that the sum() logic doesn't work. But the XOR logic will

@PASTRAMIKick

I used the Triangular number formula: ((n^2) + n) / 2, for calculating the total sum of the full range and then substracted the sum of the numbers in the given array. Ends up being O(n) for time and O(1) for space

@Martins_JC

Chat GPT me ha enviado aquí por error jajjs. Que disfrutéis el vídeo los que lo necesitáis. Saludos!!

@jjayguy23

This is so confusing to me. I've watched it several times. The solution looks simple, but something is throwing me off. What does "res = len(nums)" do?? I don't get why you changed it from 0.

@ankitasinha9912

simpe JS solution, using the sum of natural numbers concept. var missingNumber = function (nums) {
    const n = nums.length;
    const targetSum = (n * (n + 1)) / 2; //sum of n natural nos.
    //range starts from [0,n], but +0 doesn't matter
    //all distinct numbers from 0 to n;

    let currentSum=0;
    for(let i=0;i<n;i++){
        currentSum+=nums[i];
    }

    const missingNum=targetSum-currentSum;
    return missingNum;


};