Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 268 · Hashing / Anagram

Missing Number

LeetCode 268 · సులభం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

missing = n(n+1)/2 − sum(nums).

సులభంArrayHash TableMathBinary SearchBit Manipulation
LeetCode లో తెరవండి ↗

నేను Missing Number (LeetCode 268) ను ఇలా పరిష్కరించాను — Hashing / Anagram ప్యాటర్న్‌లోని ఒక సులభం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

The numbers 0..n should all be present except one. You can mark which ones you see, or use arithmetic: the sum of 0..n is n(n+1)/2, and subtracting the actual sum leaves exactly the missing number.

Approach

  1. 1

    Frequency version: mark each number, return the first unmarked index.

  2. 2

    Math version: compute the expected total of 0..n.

  3. 3

    Subtract the actual sum — the difference is the missing number.

నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు

1
Frequency tableO(n) time · O(n) space

Mark what you see.

2
Gauss sumAcceptedO(n) time · O(1) space

Expected sum minus actual.

Frequency tablePythonO(n) time · O(n) space
class Solution:
    def missingNumber(self, nums: list[int]) -> int:
        n = len(nums)
        freq_table = [0]*(n+1) 
        for num in nums:
            freq_table[num]+=1
        for i in range(n+1):
            if freq_table[i]==0:
                return i
        return -1
"""
[0,1,2,3] - 6
[3,0,1] - 4
"""
Gauss sumPythonO(n) time · O(1) space
class Solution:
    def missingNumber(self, nums: list[int]) -> int:
        n = len(nums)
        total_sum = n*(n+1)//2
        cur_sum = sum(nums)
        return total_sum-cur_sum
స్టెప్-త్రూ లోడ్ అవుతోంది…

Missing Number — తరచూ అడిగే ప్రశ్నలు

Missing Number వెనుక ఉన్న intuition ఏమిటి?

The numbers 0..n should all be present except one. You can mark which ones you see, or use arithmetic: the sum of 0..n is n(n+1)/2, and subtracting the actual sum leaves exactly the missing number.

Missing Number (LeetCode 268) ను ఎలా పరిష్కరించాలి?

1. Frequency version: mark each number, return the first unmarked index. 2. Math version: compute the expected total of 0..n. 3. Subtract the actual sum — the difference is the missing number.

Missing Number యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(1). (Accepted approach: Gauss sum.)

Missing Number లో సాధారణ పొరపాట్లు ఏమిటి?

The range is 0..n inclusive (n+1 values), not 1..n.

Missing Number ను పరిష్కరించడానికి ఒకటి కంటే ఎక్కువ మార్గాలు ఉన్నాయా?

Frequency table (O(n) time · O(n) space): Mark what you see. Gauss sum (O(n) time · O(1) space): Expected sum minus actual.