Data Structures & AlgorithmsVery hardCodeforces Div2 C · GATE CS
A circular route has gas stations with gas = [1, 2, 3, 4, 5, 2] and travel cost to the next station = [3, 4, 5, 1, 2, 3]. You start with an empty tank at one station and must complete the full circle. Which starting station index (0-based) is valid? If several valid stations exist, choose the smallest index; report −1 if none exists.
- A. -1
- B. 3
- C. 5
- D. 0
Correct answer: A. -1
Compute the total surplus: if it is negative, no start works. Otherwise, the start is just after the point where the running surplus is lowest, because any prefix before that point cannot be left with a deficit. Starting at the station with most gas is a wrong heuristic.
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
- A garden of length 5 has taps at positions 0 to 5. Tap i waters the interval [i − ranges[i], i + ranges[i]] where ranges = [3, 4, 1, 1, 0…
- From the number 1432219 you must delete exactly 3 digits (the remaining digits keep their order). What is the smallest number you can…
- What is the minimum number of swaps of two (not necessarily adjacent) elements needed to sort the array [4, 3, 2, 1, 6, 5] into increasing…
- People weigh [3, 5, 3, 4, 2, 7, 1] kg. Each boat carries at most two people and at most 9 kg in total. What is the minimum number of boats…
- Children stand in a row with ratings [1, 2, 2, 5, 4, 3, 3, 8, 1]. Each child gets at least one candy, and a child with a higher rating…
- Jobs of lengths [7, 6, 5, 4, 3, 3] are assigned to 2 identical machines. Each job runs on one machine without interruption. What is the…