Gas Station LeetCode solution in Java
1class Solution {2public int canCompleteCircuit(int[] gas, int[] cost) {3// *Upvote will be appreciated*4int totalFuel = 0;5int totalCost = 0;6int n = gas.length;7for (int i = 0; i < n; i++) {8totalFuel += gas[i];9}10for (int i = 0; i < n; i++) {11totalCost += cost[i];12}13// if totalfuel < totalCost then It is not possible to tavel14if (totalFuel < totalCost) {15return -1;16}1718// It is greather then There may be an Answer19int start = 0;20int currFuel = 0;21for (int i = 0; i < n; i++) {22currFuel += (gas[i] - cost[i]);23if (currFuel < 0) { // It Current Fuel is less than 0 mean we can't star from that index24start = i + 1; // so we start from next index25currFuel = 0;26}27}28return start;29}30}