> For the complete documentation index, see [llms.txt](https://jaywin.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jaywin.gitbook.io/leetcode/solutions/0448-find-all-numbers-disappeared-in-an-array.md).

# 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]
*/
```
