TopcoderArchiveVisit Topcoder
SRM25 Aug 2014
SRM 636

GameOfStones

Problem statement, definition, constraints, and public examples.

Problem Statement

Limak has found a large field with some piles of stones.


Limak likes perfection. It would make him happy if every pile had the same number of stones. He is now going to move some stones between the piles to make them all equal.


However, there is a catch. Limak's perfectionism does not allow him to carry just one stone at a time. As he has two hands, he must always carry exactly two stones: one in each hand. Thus, he can only make one type of an action: pick up two stones from one of the piles and carry both of them to some other pile. He is not allowed to remove a pile completely. Therefore, he cannot pick up stones from a pile that currently contains fewer than 3 stones.


You are given a int[] stones. Each element of stones is the initial number of stones in one of the piles. Compute and return the smallest number of actions Limak has to perform in order to make all piles equal. If it is impossible to make all piles equal using the allowed type of actions, return -1 instead.

Definition

Class:
GameOfStones
Method:
count
Parameters:
int[]
Returns:
int
Method signature:
int count(int[] stones)
(be sure your method is public)

Constraints

  • stones will contain between 1 and 100 elements, inclusive.
  • Each element in stones will be between 1 and 100, inclusive.

Examples

  1. {7, 15, 9, 5}
    Returns: 3
    There are four piles of stones. There are 7 stones in pile number 0, 15 stones in pile number 1, 9 stones in pile number 2, and 5 stones in pile number 3. One optimal solution looks as follows: First, Limak will move a pair of stones from pile 1 to pile 0. Afterwards, Limak will twice move a pair of stones from pile 1 to pile 3. After these 3 actions, each pile contains exactly 9 stones.
  2. {10, 16}
    Returns: -1
    It can be proven that Limak can't make these two piles equal.
  3. {2, 8, 4}
    Returns: -1
  4. {17}
    Returns: 0
    Limak doesn't need to perform any actions. There is only one pile and it means that all piles already have the same size.
  5. {10, 15, 20, 12, 1, 20}
    Returns: -1
Back to all problems