Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many integers x with 1 ≤ x ≤ 1000 are perfect squares or perfect cubes (or both)?

  1. A. 31
  2. B. 39
  3. C. 38
  4. D. 29

Correct answer: C. 38

Use inclusion–exclusion with binary-searchable roots: squares ≤ N are ⌊√N⌋ and cubes are ⌊∛N⌋ (found by binary search), and numbers that are both sixth powers are subtracted once. Counting only squares misses the cubes.

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

Exams that ask this topic