Basically, what do you do if there is no droid to the right of the door?
Obi-Wan meets Java
Arpith Siromoney
12

In this case the last droid (the arrays are sorted) is nearest to the door. The for loop now looks like:

for (int door : doors) {
int safety = 0;
for (int i = 0; i < droids.length; i++) {
if (droids[i] >= door) {
if (i == 0) safety = droids[i] — door;
else safety = Math.min(droids[i] — door, door — droids[i-1]);
if (safety > maxSafety) maxSafety = safety;
break;
} else if (i == droids.length - 1) {
safety = door - droids[i];
if (safety > maxSafety) maxSafety = safety;
break;
}
}
}

All we’re doing is setting the safety of the door to be the difference between the coordinates of the door and those of the last droid. This is only done if there is no droid to the right of the door.