# 134 Gas Station – Medium

### Problem:

There are N gas stations along a circular route, where the amount of gas at station i is gas\[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). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.

Note: The solution is guaranteed to be unique.

### Thoughts:

Here is the key thing to keep in mind.

1 If total cost is greater than total amount of gas, there is no way to make a circle travel

2 We have a current remaining which it the remaining amount of gas to start from one of the gas stations. Because there is only one unique solution. For all other gas station, there must be a point that the current remaining is negative which means it cannot start from currently marked station.

### Solutions:

```java
public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int currRemaining = 0;
        int totalRemaining = 0;
        int start = 0;
        for (int i = 0; i < gas.length; i ++){
            int remaining = gas[i] - cost[i];
            if (currRemaining < 0){
                start = i;
                currRemaining = remaining;
            }
            else{
                currRemaining += remaining;
            }
            totalRemaining += remaining;
        }//for i
        if (totalRemaining < 0)
            return -1;
        else
            return start;
    }//canComplete
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ugopireddy.gitbook.io/leet-code-solutions/solutions_101_-_150/134_gas_station__medium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
