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?
- A. 4
- B. 3
- C. 5
- 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.
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
- Given a set of intervals, you want the fewest intervals to remove so the rest do not overlap. Which greedy strategy is correct?
- Which data structure answers range-minimum queries and point updates each in O(log n) time?
- From the numbers [1, 9, 4, 7, 3, 12, 8, 5], form as many DISJOINT pairs as possible where the two numbers in each pair differ by at most 2…
- A circular binary string is "1101001010". A swap exchanges any two characters. What is the minimum number of swaps needed so that all 1s…
- Each row and each column of a 4 × 4 matrix is sorted in increasing order: [[1, 5, 9, 12], [2, 6, 10, 15], [3, 7, 11, 16], [4, 8, 13, 20]]…
- Choose a subsequence of [3, 8, 1, 9, 2, 7] (keeping order, possibly any length ≥ 1). Its alternating sum is the first chosen element minus…