`piecesRemaining = piecesAtStart - piecesTaken;`
The compiler complained about a loss of precision.
When he cast the righthand side of the expression to
`byte`, everything worked fine:

`piecesRemaining = (byte) (piecesAtStart - piecesTaken);`
So off I trudge to the Java Language Reference,
where on Page 27 I find:

So your RHS value are upcast toJava performs all integer arithmetic usingintorlongoperations. A value that is of typebyte,short, orcharis widened to anintor alongbefore the arithmetic operation is performed.

A cyclic road containsOur algorithm looked like this:ngas stations placed at various points along the route. Each station has some number of gallons of gas available. Some stations have more gas than necessary to get to the next station, but other stations do not have enough gas to get to the next station. However, the total amount of gas at thenstations is exactly enough to carry a car around the route exactly once.Your task: Find a station at which a driver can begin with an empty tank and drive all the way around the road without ever running out of gas.

The

inputto your algorithm is a list ofnintegers, one for each station. Theith integer indicates the number of miles a car can travel on the gas available at theith station.For simplicity, let's assume that stations are 10 miles apart. This means that the sum of the

nintegers will be exactly 10n.The

outputof your algorithm should be the index of the station at which the driver should begin.

for i ← 1 to n do tank ← 0 for j ← i to ((i + n) mod n) do tank := tank + gas[j] - 10 if tank < 0 then next i return iHow to do that

outer: for ( int i = 0; i < gas.length; i++ ) { tank = 0; for ( int j = i; j < i + gas.length; j++ ) { tank = tank + gas[j % gas.length] - 10; if ( tank < 0 ) continue outer; } return i; }Some days I have major epiphanies, but more often I learn some little nugget that fills an unexpected hole in the web of my knowledge. The Java gurus among you are probably thinking, "He really didn't know that? Sheesh..." Neither one of these Java nuggets is earth-shaking, just the sort of details I only learn when I need to use them in program for the first time. I suppose I could study the Java Language Reference in my free time, but I don't have the sort of job for which that sounds attractive. (Is there such a job?) -----