# Missing Number

## Problem

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.*

{% hint style="info" %}
For example:

```
Input: [4, 0, 3, 1]
Output: 2
```

```
Input: [8, 3, 5, 2, 4, 6, 0, 1]
Output: 7
```

{% endhint %}

## Solution

```
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        #1) sort the numbers in the correct position by their index
        #2) find the missing number that is not equal to it's index
        
        i=0
        while i < len(nums):
            j = nums[i] #getting the index that this number should be in
            if nums[i] < len(nums) and nums[j] != nums[i]:
                nums[j],nums[i] = nums[i],nums[j]
            else:
                i+=1
                
        for i in range(len(nums)):
            if i != nums[i]:
                return i
        return i+1
        
        
        
#Since the array has numbers in range [0, n], there
#will possibly exist a num[n] but not an index n so
#we skip this

#Time: O(n)
#Space: O(n)

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://joshualbarb.gitbook.io/leetcode-problems/cyclic-sort/missing-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
