TopCoder problem "Armies" used in TCHS SRM 27 (Division I Level Two)

Problem Statement


Two armies composed of magical creatures are facing off at a battlefield. To provide a more epic feel to the battle, the armies have agreed that each creature will attack exactly once and that only one creature may attack at a time.

Each creature has a might trait assigned to it. The order in which the creatures attack is uniquely determined by the following rules:

  • Creatures with higher might attack before creatures with lower might.
  • If creatures have equal might, attacking creatures alternate between armies. For example, if the last creature to attack was from the first army and the next creature could be from either army, a creature from the second army will attack next. If the very first attack is to be decided, a creature from the first army goes first.
  • If more than one creature in the same army has the same might, the creature given earlier in the input goes first.

You will be given the descriptions of the armies as two String[]s, army1 and army2. Each element of army1 and army2 will be formatted as "NAME MIGHT" (quotes for clarity), where NAME is the name of a creature, and MIGHT is an integer, the might of that creature. Return a String[] containing the creatures' names in the order in which they attack.



Parameters:String[], String[]
Method signature:String[] findOrder(String[] army1, String[] army2)
(be sure your method is public)


-army1 and army2 will each contain between 0 and 50 elements, inclusive.
-Each element of army1 and army2 will be formatted as "NAME MIGHT" (quotes for clarity).
-NAME in each element of army1 and army2 will be a string containing between 1 and 20 uppercase letters ('A'-'Z'), inclusive.
-MIGHT in each element of army1 and army2 will be an integer between 1 and 999, inclusive, without leading zeroes.
-No two creatures will have the same name.


{ "DRAGON 10" }
{ "BOAR 1", "ELF 3" }
Returns: {"DRAGON", "ELF", "BOAR" }
The dragon is by far the mightiest and attacks first. The elf follows and the boar goes last.
{ "CENTAUR 2", "ELF 3" }
The swordsman attacks first. The next creature could be either the archer or the elf because both have might 3. Since the first creature to attack was from the first army, the next will be the elf from the second army. The archer and centaur follow.
{ "ARCHER 5", "PIXIE 3" }
{ "OGRE 10", "WOLF 10", "GOBLIN 3" }
Returns: {"OGRE", "WOLF", "ARCHER", "GOBLIN", "PIXIE" }
{ "A 6", "B 7" }
{ }
Returns: {"B", "A" }

Problem url:

Problem stats url:




PabloGilberto , brett1479 , Olexiy

Problem categories:

Simulation, Sorting