Bitmask From Indices
Python
Medium
4 views
Problem Description
Read n and then k indices (0-based). Build a bitmask with those bits set and output it as integer.
Input Format
First line n k. Second line: k indices.
Output Format
One integer mask.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<2: sys.exit(0)
n=int(p[0]); k=int(p[1])
mask=0
for i in range(k):
idx=int(p[2+i])
mask |= (1<<idx)
sys.stdout.write(str(mask))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!