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?
- A. 19
- B. 21
- C. 18
- 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.
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 non-empty subsets of [2, 4, 6, 8, 10, 12, 3] (chosen by position) have a sum of exactly 25?
- The numbers 1, 2, 3, 4, 5 are pushed onto an initially empty stack in this order, and pops may be interleaved with pushes at any time…
- A singly linked list holds 1 to 10 in order. Every block of k = 4 consecutive nodes is reversed in place, but a final block with fewer…
- The integers 1, 2, 3, 4, 5, 6 are pushed onto an initially empty stack in this order, and pops may be interleaved with pushes at any time…
- In a linked list the first 5 nodes form a tail, and the next 7 nodes form a cycle (the last node points back to the 6th node). Floyd's…
- For each element of the array [6, 2, 5, 4, 7, 3, 1, 8, 2] find the next greater element to its right (the value -1 if none exists). What…