(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
Really appreciate the fact that you explain multiple solutions. Always interesting to learn them!
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)
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
Thanks!
I'm glad that math finally kicked in for me, remembering Gauss's formula for sums.
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)
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 🙂
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
result = len(nums) for i, n in enumerate(nums): result += i-n return result
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!
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)"
I had no idea XOR was an operator in python ! WOW !
The clearest explanation I've seen on this. Hot stuff. Thanks for your videos, they are the bomb.
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
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
Chat GPT me ha enviado aquĂ por error jajjs. Que disfrutĂ©is el vĂdeo los que lo necesitáis. Saludos!!
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.
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; };
@supercarpro