P433: Optimized Java solution and added explanation.