# 0448. Find All Numbers Disappeared in an Array

<https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array>

## Description

Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return *an array of all the integers in the range* `[1, n]` *that do not appear in* `nums`.

**Example 1:**

```
**Input:** nums = [4,3,2,7,8,2,3,1]
**Output:** [5,6]
```

**Example 2:**

```
**Input:** nums = [1,1]
**Output:** [2]
```

**Constraints:**

* `n == nums.length`
* `1 <= n <= 105`
* `1 <= nums[i] <= n`

**Follow up:** Could you do it without extra space and in `O(n)` runtime? You may assume the returned list does not count as extra space.

## ac

```java
class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        // corner cases
        if (nums.length == 0) return res;

        // iterate 
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            while (nums[i] < n && i != nums[i] && nums[nums[i]] != nums[i]) {
                swap(nums, i, nums[i]);
            }
            // special case, n
            if (nums[i] == n) swap(nums, 0, i);
        }

        // final check get result
        if (nums[0] != n) res.add(n);
        for (int i = 1; i < n; i++) {
            if (nums[i] != i) {
                res.add(i);
            }
        }

        return res;
    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

/*
make use of index, make nums[i] = i, careful: if nums[i] == n, swap to nums[0]
*/
```


---

# 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://jaywin.gitbook.io/leetcode/solutions/0448-find-all-numbers-disappeared-in-an-array.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.
