Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

How many non-empty subsets of [1, 2, 3, 4, 5, 6, 7] (chosen by position) have a sum that is a perfect square?

  1. A. 19
  2. B. 21
  3. C. 18
  4. D. 16

Correct answer: A. 19

Enumerate the 127 non-empty subsets and test each sum with an integer square-root check. Every subset sum lies between 1 and 28 (the total), so only the squares 1, 4, 9, 16 and 25 can occur; counting the subsets with each of those sums gives the answer. Counting only single-element squares misses the combined subsets.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic