38 lines
896 B
Java
38 lines
896 B
Java
|
|
public class Balls {
|
|
|
|
enum Color {green, red};
|
|
|
|
public static void main (String[] param) {
|
|
// for debugging
|
|
}
|
|
|
|
public static void reorder (Color[] balls) {
|
|
int redBallCount = 0;
|
|
|
|
// count how many red balls there is
|
|
for (int i = 0; i < balls.length; i++) {
|
|
if (balls[i] == Color.red) {
|
|
redBallCount++;
|
|
}
|
|
}
|
|
|
|
|
|
// If all balls are red or green then just return
|
|
if (redBallCount == balls.length) {
|
|
return;
|
|
} else if (redBallCount == 0) {
|
|
return;
|
|
}
|
|
|
|
// replace the contents of the array with new colors that are in correct order
|
|
for (int i = 0; i < redBallCount; i++) {
|
|
balls[i] = Color.red;
|
|
}
|
|
for (int i = redBallCount; i < balls.length; i++) {
|
|
balls[i] = Color.green;
|
|
}
|
|
}
|
|
}
|
|
|