Data Structures & AlgorithmsHardGATE CS · ICPC regional

Gas stations are on a circular route: gas = [1, 2, 3, 4, 5] and the cost to reach the next station = [3, 4, 5, 1, 2]. Starting with an empty tank, from which station (0-based index) can you complete the circuit?

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

Correct answer: B. 3

Track the running tank; when it goes negative, restart at the next station. If total gas is at least total cost, the last restart point works.

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

Exams that ask this topic