Missing Number
Missing Number
Section titled “Missing Number”
Easy
Day 3 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Examples & Constraints
Section titled “Examples & Constraints”Example 1:
- Input:
nums = [3,0,1] - Output:
2
Constraints:
n == nums.length1 <= n <= 10^40 <= nums[i] <= n
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”Sum formula n * (n + 1) / 2 minus array sum gives missing number.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Gauss Sum / Bitwise XOR
📊 Step-by-Step Execution (Mermaid Diagram)
Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”graph TD A["Input Integer / Bits"] --> B["Apply Bitwise Operation (AND / XOR / Shift)"] B --> C{"Check Bit Condition"} C -- "Condition Met" --> D["Update Bit Count / Result"] C -- "Continue" --> E["Shift Bits (>>> 1 or & n-1)"] E --> B D --> F["Return Final Result"]🐢 Brute Force Solution
Section titled “🐢 Brute Force Solution”function missingNumber(nums) { const n = nums.length; let expected = (n * (n + 1)) / 2; let actual = nums.reduce((a, b) => a + b, 0); return expected - actual;}- Time Complexity:
O(n) - Space Complexity:
O(1) - Explanation: Gauss sum.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function missingNumber(nums) { const n = nums.length; let expected = (n * (n + 1)) / 2; let actual = nums.reduce((a, b) => a + b, 0); return expected - actual;}- Time Complexity:
O(n) - Space Complexity:
O(1) - Explanation: Gauss summation formula.
🐾 Step-by-Step Walkthrough
Section titled “🐾 Step-by-Step Walkthrough”- Initialize State: Setup necessary pointers, dynamic programming arrays, or hash maps.
- Iterate & Evaluate: Process the input according to the boundary conditions.
- Update & Return: Compute the optimal answer and return early or at termination.
🎙️ FAANG Interview Pitch
Section titled “🎙️ FAANG Interview Pitch”Subtract total array sum from total expected sum n*(n+1)/2.
💡 Progressive Hints
Section titled “💡 Progressive Hints”- Sum from 0 to n is n * (n + 1) / 2.