Statistics

Problem Statement for "TransformArray"

Problem Statement

We have an array of positive integers. We will transform this array by repeating the following operation until there are less than two elements left:
  1. Choose the two elements that have the minimal absolute difference. If there are multiple such pairs, choose the one among them with the minimal sum. If there are still multiple pairs, choose any one of them.
  2. Decrease both elements in the pair by 1.
  3. Remove all zeros from the array.
It's easy to see that this process always ends in a finite number of steps.

For example, suppose we have an array of four integers {3, 2, 3, 2}. The transformation process goes like this:
Step 1: (3, 2, 3, 2) --> (3, 1, 3, 1) (decreasing values 2 and 2)
Step 2: (3, 1, 3, 1) --> (3, 3) (decreased elements became zeros, so we removed them)
Step 3: (3, 3) --> (2, 2)
Step 4: (2, 2) --> (1, 1)
Step 5: (1, 1) --> ()
Thus, we have an empty array at the end of the process.

You are given a int[] elements which represents the array to transform. Return the number of steps in the transformation process.

Definition

Class:
TransformArray
Method:
doTransform
Parameters:
int[]
Returns:
int
Method signature:
int doTransform(int[] elements)
(be sure your method is public)

Constraints

  • elements will contain between 1 and 50 elements, inclusive.
  • Each element of elements will be between 1 and 1000, inclusive.

Examples

  1. {3,2,3,2}

    Returns: 5

    The example from the problem statement.

  2. {3}

    Returns: 0

    There is already just one element in the array, so we do not have to perform the transformation at all.

  3. {1,2,3,4}

    Returns: 4

    The transformation process goes like this: {1, 2, 3, 4} --> {1, 3, 4} --> {1, 2, 3} --> {1, 3} --> {2}

  4. {146,235,193,395,906,421,308,123,538,659,520,499,997,795,185,656,439,842,291,886,851,73,604,809,52,947,13,552,795,562,449,749,950}

    Returns: 8419

  5. {371,594,265,111,645,305,182,820,397,979,540,684,788,876,674,336,574,489,374,51,468,790}

    Returns: 5364

  6. {938,551,359,181,768,215,848,446,19,200,900,741,754,598,641,818,101,625,750,822,284,694,514,8,425,366,248,270,353,336,929}

    Returns: 7648

  7. {781,980,254}

    Returns: 781

  8. {154,173,177,814,701,957,153,36,822,48,251}

    Returns: 1902

  9. {812,91,546,435,839,284,68,175,905,10,517,756,656,839,217,914,88,63,887,273,257,394,68,194,977,690,683,244,586,933,884,124,49,957,619,190,964,653,583,896,258,631}

    Returns: 10271

  10. {767,172,456,260,755,664,987,701,92,32,285,744,397,726,171,107,138,208,305,576,347,951,353,732,974,639,181,889,238}

    Returns: 6674

  11. {279,245,777,959,864,506,244,611,182,757,690,241,566,50,53,698,818,944,615,561,420,491,603,572,859,806,740,828,807,904,109,551,138,718,945,470,18,480,202,104,543,873,17,521,677,635,427,334,461}

    Returns: 12666

  12. {634,2,354,108,115,33,733,441,601,756,691,409,688,120,631,65,544,219,25,535,383,832,58,938,519,881,852,184,799,786,181,264,465,959,868,876,736,906,254,508,664,694,448,336,57,745,249,543,512,208}

    Returns: 11963

  13. {33,620,193,805,747,833,184,33,384,208,488,158,320,844,471,122,562,12,380,370,116,412,813,124,54,949,782,673,643,825,381,596,152,1,253,92,186,313,382,837,665,802,915,44,802,42,10,641,696,530}

    Returns: 10497

  14. {948,583,967,890,513,927,39,844,459,609,571,839,792,388,704,73,680,777,137,457,497,461,157,441,371,243,281,945,291,737,706,264,57,322,962,50,626,55,466,727,651,561,869,968,261,110,402,62,376,379}

    Returns: 12519

  15. {1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000}

    Returns: 25000

  16. {1,1,1,1,1,2,2,2,2,1000}

    Returns: 7

  17. {10,6,3,7,7,9,5,1,8,4}

    Returns: 27

  18. {3, 2, 2 }

    Returns: 2

  19. {3 }

    Returns: 0

  20. {10, 6, 3, 7, 7, 9, 5, 1, 8, 4 }

    Returns: 27

  21. {1, 1, 1, 1, 1, 1, 1 }

    Returns: 3

  22. {2, 1, 1 }

    Returns: 1

  23. {7, 9, 10 }

    Returns: 9

  24. {1, 2, 3 }

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: