How to pick exactly k bits from a Java BitSet of length m with n bits turned on, where k≤n≤m?
k
m
n
k≤n≤m
Example input: m=2
m=2
You could scan the set from the first bit to the last, and apply reservoir sampling to the bits that are set.
The algorithm has O(m) time complexity, and requires O(k) memory.
O(m)
O(k)