-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGasStation.java
More file actions
25 lines (24 loc) · 790 Bytes
/
GasStation.java
File metadata and controls
25 lines (24 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class GasStation {
public static void main(String[] args) {
int gas[] = { 1, 2, 3, 4, 5 };
int cost[] = { 3, 4, 5, 1, 2 };
int startingPoint = 0;
int surplus = 0;
int deficit = 0;
for (int i = 0; i < gas.length; i++) {
surplus = deficit += gas[i] - cost[i];
// deficit = deficit + (gas[i]-cost[i]);
// deficit += gas[i] - cost[i];
surplus = deficit;
if (surplus < 0) {
surplus = 0;
startingPoint++; // Move to the Next Station
}
}
if (deficit >= 0) {
System.out.println("Starting Station " + startingPoint);
} else {
System.out.println("No Station Found...");
}
}
}