TopcoderArchive
SRM24 Jan 2006
SRM 285

BasketsWithApples

Problem statement, definition, constraints, and public examples.

Problem Statement

We have some baskets containing apples, and we would like to perform the following procedure in a way that maximizes the number of remaining apples. First, we discard some (or none) of the baskets completely. Then, if the remaining baskets do not all contain the same number of apples, we remove excess apples from the baskets until they do.

You will be given a int[] apples where the i-th element of apples is the number of apples in the i-th basket. Return the number of apples remaining after the procedure described above is performed.

Definition

Class:
BasketsWithApples
Method:
removeExcess
Parameters:
int[]
Returns:
int
Method signature:
int removeExcess(int[] apples)
(be sure your method is public)

Constraints

  • apples will contain between 1 and 50 elements, inclusive.
  • Each element in apples will be between 0 and 1000, inclusive.

Examples

  1. {1, 2, 3}
    Returns: 4
    We should remove the first basket and leave two apples in each of the two remaining baskets.
  2. {5, 0, 30, 14}
    Returns: 30
    We should leave only the third basket.
  3. {51, 8, 38, 49}
    Returns: 114
  4. {24, 92, 38, 0, 79, 45}
    Returns: 158
Back to all problems