Missing Number XOR
Python
Medium
3 views
Problem Description
Read n-1 numbers from 1..n (one is missing). Compute the missing number using XOR.
Input Format
First line n. Second line n-1 integers.
Output Format
One integer missing.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
arr=list(map(int,p[1:]))
x=0
for i in range(1,n+1):
x^=i
for v in arr:
x^=v
sys.stdout.write(str(x))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!