Intermediate · Sets · 143 of 264
The missing number
The numbers from 1 to n are given in some order, but one is missing. Find it.
Input: n on the first line, then the other n - 1 numbers on one line. Output: the missing number.
Examples
Input
5 3 1 5 2
Output
4
Input
2 2
Output
1
Input
6 2 3 4 5 6
Output
1
One more test checks other inputs when you press Check.
Stuck? The idea behind sets
A set holds each value once, in no particular order: set(list) drops repeats at once.
Checking whether a value is in a set is fast. & keeps what two sets share, | joins them, - keeps what is only in the first.
a = set(input().split()) b = set(input().split()) print(sorted(a & b))
Watch for: A set has no order: sort it (sorted()) before printing, or the order may change.