Editorial for Missing Number


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Penguin60

C++


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector<int> v(n+1, 0);

    for (int i = 0; i < n-1; i++) {
        int x; cin >> x;
        v[x]++;
    }

    v[0] = 1;

    auto ind = find(v.begin(), v.end(), 0);

    cout << distance(v.begin(), ind) << "\n";
}

Java


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] v = new int[n + 1];

        for (int i = 0; i < n - 1; i++) {
            int x = sc.nextInt();
            v[x]++;
        }

        v[0] = 1;

        int index = -1;
        for (int i = 0; i < v.length; i++) {
            if (v[i] == 0) {
                index = i;
                break;
            }
        }

        System.out.println(index);
    }
}

Python


n = int(input())
nums = list(map(int, input().split()))
v = [0] * (n + 1)

for x in nums:
    v[x] += 1

v[0] = 1
print(v.index(0))

Comments

There are no comments at the moment.