MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / canCompleteCircuit

Method canCompleteCircuit

GasStation.java:2–17  ·  view source on GitHub ↗
(int[] gas, int[] cost)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int canCompleteCircuit(int[] gas, int[] cost) {
3
4 int index=0,tank=0,total=0;
5 for(int i=0;i<gas.length;i++)
6 {
7 int consume=gas[i]-cost[i];
8 tank+=consume;
9 if(tank<0)
10 {
11 index=i+1;
12 tank=0;
13 }
14 total+=consume;
15 }
16 return (total<0)?-1:(index);
17 }
18}
19
20

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected