Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Arrays

Find All Missing Numbers

Easy · Arrays · Asked at Google, Amazon, Microsoft, TCS, Infosys

An array of n integers has every value between 1 and n; some values appear twice and others are missing. Print all the missing values in increasing order, or NONE if nothing is missing.

Input

First line: n. Second line: n space-separated integers.

Output

The missing values, space-separated, or NONE.

Constraints

Use the array itself as the "seen" table: for each value v, make a[v − 1] negative. Indices whose value stays positive are missing — O(n) time, O(1) extra space.

Example 1

Input

8
4 3 2 7 8 2 3 1

Output

5 6

Example 2

Input

2
1 1

Output

2

Hints

  1. Values are in 1 … n, so each value can be mapped to an index.
  2. Mark index v − 1 as seen by making the number there negative.
  3. Indices that stay positive belong to missing values.

Approach

For every element, take v = |a[i]| and make a[v − 1] negative (if it is not already). After the pass, every index j with a[j] > 0 means the value j + 1 never appeared. This reuses the input as the "seen" array, so the extra space is O(1). Print NONE if no index stays positive.

Complexity: O(n) time · O(1) extra space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    for (int i = 0; i < n; i++) {
        int v = abs(a[i]);
        if (a[v - 1] > 0) a[v - 1] = -a[v - 1];
    }
    string out;
    for (int j = 0; j < n; j++)
        if (a[j] > 0) out += (out.empty() ? "" : " ") + to_string(j + 1);
    cout << (out.empty() ? "NONE" : out) << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n = data[0]
a = data[1 : 1 + n]
for x in a:
    v = abs(x)
    if a[v - 1] > 0:
        a[v - 1] = -a[v - 1]
missing = [j + 1 for j in range(n) if a[j] > 0]
print(" ".join(map(str, missing)) if missing else "NONE")

Java solution

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            a[i] = (int) in.nval;
        }
        for (int i = 0; i < n; i++) {
            int v = Math.abs(a[i]);
            if (a[v - 1] > 0) a[v - 1] = -a[v - 1];
        }
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < n; j++)
            if (a[j] > 0) {
                if (sb.length() > 0) sb.append(' ');
                sb.append(j + 1);
            }
        System.out.println(sb.length() == 0 ? "NONE" : sb.toString());
    }
}

Solve Find All Missing Numbers online · All practice problems