Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many integers x with 1 ≤ x ≤ 1000 are perfect squares or perfect cubes (or both)?
- A. 31
- B. 39
- C. 38
- 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.
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
- Intervals (inclusive integer ranges) are [(1, 3), (2, 5), (4, 9), (6, 7), (8, 12), (9, 10)]. What is the minimum number of intervals whose…
- A binary string is "10110010". Flipping position i costs weight[i] = [3, 1, 4, 1, 5, 9, 2, 6]. You want the string to alternate (0101…
- How many contiguous subarrays of the binary array [1, 0, 1, 1, 0, 0, 1, 0] contain equally many 0s and 1s? (Non-empty subarrays only.)
- A string of brackets is ')))(()(' where '(' opens and ')' closes. What is the minimum number of brackets that must be removed to make the…
- How many elements of [2, 5, 1, 4, 3] have no strictly smaller element anywhere to their right?
- Three processes arrive at time 0 with bursts 5, 2 and 7. Round-robin uses quantum 3 and the queue starts as P1, P2, P3. Which process…