Bitmask DP

You've handled linear, 2D, and tree states. Bitmask DP tracks which elements you've used in a subset.

42 lessons
206 min
Codeforces: 1500-2200LeetCode: 1700-2100

Lessons

1. Intro

The Goal

5m

2. Vocabulary - Bitmask

Subset as integer

5m

3. Bit Operations

The tools you need

5m

4. Simple Example - Subset Membership

Concrete usage

6m

5. Iterating Subsets

Enumeration patterns

5m

6. Quiz: Bit Operations

Knowledge check

3m1 problems

7. The Bitmask DP Pattern

Dp[mask] indexed by subsets

6m

8. When Bitmask DP?

Recognition tips

8m

9. Quiz: When Bitmask DP

Knowledge check

3m1 problems

10. Subset Sum - Problem Statement

Entry problem

3m1 problems

11. Subset Sum - Key Observation

Bitmask as subset

5m

12. Subset Sum - Implementation

The code

6m1 problems

13. Quiz: Subset Sum

Knowledge check

3m1 problems

14. LeetCode 1879 Minimum XOR Sum of Two Arrays - Problem Statement

LeetCode 1879

5m1 problems

15. Assignment - key observation

Process in order

5m

16. Assignment - State Design

Dp[mask] alone

6m

17. Assignment - Implementation

O(n × 2^n)

5m1 problems

18. Quiz: Assignment

Knowledge check

3m1 problems

19. Hamiltonian Flights - Problem Statement

Count routes, bitmask DP

5m1 problems

20. TSP - key observation

Visited set + current city

5m

21. TSP - State Design

Dp[mask][last]

5m

22. TSP - Transition

Add next city

5m

23. TSP - Implementation

O(n² × 2^n)

5m1 problems

24. Quiz: TSP

Knowledge check

3m1 problems

25. LeetCode 698 Partition to K Equal Sum Subsets - Problem Statement

LeetCode 698

5m1 problems

26. K Subsets - key observation

Track partial sum

5m

27. K Subsets - Implementation

The code

5m1 problems

28. Why Sum Over Subsets?

A new problem type

5m

29. Iterating Submasks

The O(3^n) way

6m

30. SOS DP - The Core Idea

Build subsets bit by bit

5m

31. SOS DP - Transition

Include or exclude bit i

6m

32. Quiz: SOS DP Transition

Knowledge check

3m1 problems

33. SOS DP - Space Optimization

Drop the second dimension

5m

34. SOS DP - Implementation

The code

6m

35. Bit Problem - Problem Statement

CSES 1654

8m1 problems

36. Bit Problem - Query Analysis

Subset relationships

6m

37. Bit Problem - Implementation

Three SOS passes

8m1 problems

38. SOS DP - Superset Version

The dual problem

6m

39. Quiz: SOS DP Applications

Knowledge check

3m1 problems

40. When to Use SOS DP

Recognition and practice

3m

41. Lessons from Bitmask DP

summary

3m

42. Section Recap

What we learned

3m

Practice Problems

1.

Classic TSP variant using BFS with bitmask

2.
FishCodeforceshard

Combines probability with bitmask DP elegantly

3.

Covered with full walkthrough in this section.

4.

Covered with full walkthrough in this section.

5.

Covered with full walkthrough in this section.

6.

Covered with full walkthrough in this section.

7.

Covered with full walkthrough in this section.

8.
VowelsCodeforces

Covered with full walkthrough in this section.

9.
Bits And PiecesCodeforces

Covered with full walkthrough in this section.

10.

Classic bitmask DP with primes. Track which primes are used to ensure coprimality.

11.
Kefa and DishesCodeforcesmedium

Choose k dishes with satisfaction bonuses. TSP variant with subset selection.

12.
Make It OneCodeforceshard

Find minimum subset with GCD 1. Inclusion-exclusion with bitmask over prime factors.

13.
Compatible NumbersCodeforcesmedium

Find pair with AND = 0. SOS DP to propagate answers through submasks.

14.
Bits And PiecesCodeforceshard

Maximize a[i] OR (a[j] AND a[k]). SOS DP tracking two rightmost indices per mask.

15.
x-prime SubstringsCodeforceshard

Remove minimum chars to avoid x-prime substrings. Aho-Corasick + bitmask DP.

16.

Partition tasks into sessions. Bitmask DP tracking remaining time per session.

17.

Assign hats to people uniquely. Bitmask over people, iterate over hats.

18.

Connect all points in both groups. Bitmask DP over second group connections.

19.

TSP on strings - minimize total length. Classic bitmask DP with path reconstruction.

20.

Count perfect matchings using bitmask DP - classic application of subset enumeration.

21.

Partition into groups maximizing total score - subset DP iterating over all submasks.

22.

Count permutations satisfying < or > constraints - teaches insertion DP pattern.

Ready to start learning?

Access all 42 lessons with interactive content and progress tracking.