AfterAcademy Tech
•
02 Jun 2020

Difficulty: Medium
Asked in: Amazon, Google
Problem Description
There are N gas stations along a circular route, where the amount of gas at station i is arr[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). At the beginning of the journey, the tank is empty at one of the gas stations.
Return the minimum starting gas station’s index if you need to travel around the circuit once, otherwise return -1.
Problem Note
i and ending up at i again.Example 1
Input:
arr[] = [1,2,3,4,5]
cost[] = [3,4,5,1,2]
Output: 3
Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Gas available in Tank = 0 + 4 = 4
Travel to station 4.Gas available in tank = 4 - 1 + 5 = 8
Travel to station 0.Gas available in tank = 8 - 2 + 1 = 7
Travel to station 1.Gas available in tank = 7 - 3 + 2 = 6
Travel to station 2.Gas available in tank = 6 - 4 + 3 = 5
Travel to station 3.The cost is 5.Your gas is just enough to travel back to station 3.Therefore, return 3 as the starting index.
Example 2
Input:
arr[] = [2,3,4]
cost[] = [3,4,3]
Output: -1
Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Gas available in tank = 0 + 4 = 4
Travel to station 0.Gas available in tank = 4 - 3 + 2 = 3
Travel to station 1.Gas available in tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.Therefore, you can't travel around the circuit once no matter where you start.
We will be discussing a brute force and a greedy solution for the problem
For each station i choose it as a starting point, and try to visit every other station and maintain a fuel to that will tell about the possibility of visiting the next station, if not then choose some other starting station and try to visit until you have reached the starting station.
Solution Steps
Pseudo Code
int canCompleteCircuit(int[] gas, int[] cost) {
n = gas.size()
for (int i = 0 to i < n) {
fuel = gas[i]
bool isAmple = true // determine a valid starting point
for (int j = 0 to j < n) {
currStation = (i + j) % n
nextStation = (currStation + 1) % n
fuel = fuel - cost[currStation]
// not reachable from currStation to nextStation
if (fuel < 0) {
isAmple = false
break
}
// refuel in nextStation
fuel = fuel + gas[nextStation]
}
if (isAmple) {
return i
}
}
return -1
}
Complexity Analysis
Time Complexity: O(n²)
Space Complexity: O(1)
Critical Ideas To Think
isAmple represents in the pseudo code?currStation to nextStation?The problem enables you to have these two ideas:
The proof of the above two ideas:
This Concludes:
Solution Steps
start to store the valid starting index from where the car could touch all the stations.i, fill the fuel tank with gas[i] and burn the fuel by cost[i]< 0 then, choose the next index as the starting point.startPseudo Code
int canCompleteCircuit(int[] gas, int[] cost) {
start = 0
total = 0
tank = 0
for( i = 0 to i < gas.size() ) {
tank = tank + gas[i] - cost[i]
if(tank < 0) {
start = i+1
total= total + tank
tank=0
}
}
if(total + tank < 0) {
return -1
} else {
return start
}
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Critical Ideas To Think
tank becomes < 0 we set the next index as the start. Why?tank becomes < 0, We then set it to 0 . Why?total + tank < 0 ?
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
This is an interview problem asked by companies like Amazon. This problem is based on Greedy Algorithm and is one of the very basic problem for understanding Greedy Algorithms.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft, Yahoo and Adobe. The problem is to design a stack that will support push(), pop(), top() and getMin() operation in constant time. We will be discussing two different ways to approach this problem.

AfterAcademy Tech
In this blog, we will learn about the classic reader-writer problem in Operating System. We will also see how to remove this problem in Operating System.

AfterAcademy Tech
In this blog, we will learn about the Producer-Consumer problem in Operating System and we will also learn how to solve this problem. It is used in multi-process synchronization.
