Practice Discrete Math

Algorithms / Subset Enumeration Bitmask

Least You Need to Know: Bitmask Subset Enumeration and Used-Set State

Enumerating masks from `0` to `2^n - 1` gives every subset of an `n`-element set. Interviews use this for subset generation, used-element state, and small-state dynamic programming where each bit records a chosen item.

جو کم از کم جاننا ضروری ہے

اہم علامتیں

0 ... 2^n - 1 all subset masks for n items
mask >> i & 1 whether item i is included
full_mask mask with the low n bits all set

مختصر حل شدہ مثال

  • For three items, masks `000` through `111` cover all subsets.
  • Mask `101` means take items 0 and 2 but not 1.
  • A loop over all masks therefore enumerates all subsets.
  • The same idea tracks which jobs or cities have already been used in a state-space search.

عام غلطیاں

اس قسم کے سوال کو کیسے پہچانیں

Next recommended lesson

Continue through this topic with Least You Need to Know: Subsets, Combinations, and Choose/Skip Search.

Least You Need to Know: Subsets, Combinations, and Choose/Skip Search

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

مشق شروع کریں