Data Structures & AlgorithmsVery hardICPC regional · GATE CS
The universe is {1, ..., 8} and the available sets are [[1, 2, 3], [3, 4, 5], [5, 6, 7], [1, 7, 8], [2, 4, 6, 8]]. What is the minimum number of these sets whose union covers the whole universe?
- A. 4
- B. 5
- C. 3
- D. 2
Correct answer: C. 3
This is set cover, which is NP-hard in general, so the answer is found by checking subsets (bitmask over the sets). Choosing the set covering the most uncovered elements each time is a fast greedy that can overshoot the optimum.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many subsets of {1, 2, ..., 7} have a sum exactly equal to half of 1 + 2 + ... + 7? (The complementary subset is counted separately.)
- How many non-empty subsets of [2, 3, 4, 5, 6] (chosen by position) have a sum that is a prime number?
- Five bitmasks ['0b11', '0b110', '0b1100', '0b1001', '0b101'] are available. Choose the fewest of them whose bitwise OR equals 0b1111. What…
- How many index triples i < j < k of [-4, -1, 0, 2, 3, -2, 5, 1] have a[i] + a[j] + a[k] = 0?
- How many unordered pairs of positions in [12, 21, 30, 4, 13, 9, 102] have elements with the same decimal digit sum?
- How many binary strings of length 7 contain exactly 3 zeros and NO run of three equal consecutive digits (neither 000 nor 111 appears)?