Missing Number | XOR + Math Sum Formula | JavaScript | LeetCode 268

Опубликовано: 13 Май 2026
на канале: Ashwini Kemshetty
9
2

📝 Description:
🔥 Day 91 of my LeetCode Daily Challenge! In this problem, I solve LeetCode 268 - Missing Number in JavaScript using two efficient approaches:

XOR approach
Sum formula approach

This is a classic easy-to-medium interview problem where we are given an array containing n distinct numbers from the range [0, n], and we need to find the one number that is missing.

🧠 Approach 1: XOR

The XOR trick works because:

a ^ a = 0
a ^ 0 = a

So if we XOR:

all indices from 0 to n
and all numbers in the array

all matching values cancel out, and the missing number remains.

✅ Time Complexity: O(n)
✅ Space Complexity: O(1)

🧠 Approach 2: Sum Formula

We know the expected sum of numbers from 0 to n is:

n(n+1)/2

Subtract all array elements from this expected sum, and the remaining value is the missing number.

✅ Time Complexity: O(n)
✅ Space Complexity: O(1)

🚀 Why these approaches are good:
Both solutions avoid sorting and use constant extra space, making them clean and interview-friendly.

XOR is a great bit manipulation trick
Math formula is simple and intuitive


📚 In this video, you will learn:
How to solve Missing Number using XOR
How XOR cancellation works
How to solve it using the sum formula
Why both approaches are optimal
How to choose between math and bit manipulation in interviews

🔗 Problem Link:
https://leetcode.com/problems/missing...

If you found this helpful, don’t forget to like, comment, and subscribe for more daily LeetCode solutions, JavaScript DSA problems, and coding interview prep 🚀

#LeetCode #JavaScript #DSA #Day91 #MissingNumber #LeetCode268 #XOR #BitManipulation #Math #CodingInterview #Algorithms #DataStructures #ProblemSolving #LeetCodeJavaScript #100DaysOfCode