Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

Asteroids move in a row; positive values move right and negative values move left, and when two meet the one with the smaller absolute value explodes (equal sizes both explode). Asteroids moving the same way never meet. How many asteroids survive from [5, 10, -15, 4, -2, 8, -8]?

  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

Use a stack: a left-moving asteroid fights the positive ones on the stack top-first, and it survives only if it beats all of them it meets.

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

Exams that ask this topic