Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE
Problem: The array [2, 3, 4, 5, 6, 7, 8] is examined for the number of subsets whose sum is a perfect square (the empty subset has sum 0, which counts as a perfect square; subsets are counted by their chosen positions).
- A. 16
- B. 15
- C. 5
- D. 128
Correct answer: A. 16
Brute force over the 128 subsets finds 16 with square sums (0, 4, 9, 16 or 25), including the empty one. Leaving it out gives 15, counting distinct square sums gives 5, and 128 counts all subsets.
More Coding Interview Problems questions
- Problem: In the string "xyxyxy", how many substrings of length 4 are palindromes?
- Problem: A sequence of 10 operations applies to an initial value of 0: operation i (1 ≤ i ≤ 10) adds i if i is odd and multiplies the…
- Problem: A graph's adjacency list is 0: [1, 2], 1: [3], 2: [3, 4], 3: [5], 4: [5], 5: []. What is the number of distinct topological…
- Problem: The number of integers from 1 to 100 that have exactly three divisors is what value? (Numbers with exactly three divisors are…
- Problem: What is the number of inversions in the array [4, 3, 2, 1, 0]?
- Problem: The string "cabbage" is sorted by letter using a stable sort on the letters only. What is the 4th character of the sorted string?