알고리즘/CSES
[C++] [CSES] Missing Number (Introductory Problems)
조명인
2023. 2. 9. 10:37
CSES의 두번째 문제입니다.
자연수 n을 입력받은 후, n-1개의 자연수를 입력받습니다.
나머지 한 개의 자연수를 출력해야 합니다.
Input
The first input line contains an integer n.
The second line contains n−1 numbers. Each number is distinct and between 1 and n (inclusive).
Output
Print the missing number.
Constraints
- 2≤n≤200000
Example
Input:
5
2 3 1 5
Output:
4
Code
#include<bits/stdc++.h>
using namespace std;
int n, num, ret;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin >> n;
vector<int> ar(n + 1, 0);
for (int i=1;i<n;i++){
cin >> num;
ar[num] = 1;
}
for (int i=1;i<=n;i++){
if (ar[i] == 0) ret = i;
}
cout << ret;
}
생각해야 하는 것
- 해시 테이블이나 배열을 선언하고 쭉 받은 다음 한 번의 for문을 돌면 됩니다.