Problem Statement
- 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.
- Decrease both elements in the pair by 1.
- Remove all zeros from the array.
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
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
{3,2,3,2}
Returns: 5
The example from the problem statement.
{3}
Returns: 0
There is already just one element in the array, so we do not have to perform the transformation at all.
{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}
{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
{371,594,265,111,645,305,182,820,397,979,540,684,788,876,674,336,574,489,374,51,468,790}
Returns: 5364
{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
{781,980,254}
Returns: 781
{154,173,177,814,701,957,153,36,822,48,251}
Returns: 1902
{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
{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
{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
{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
{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
{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
{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
{1,1,1,1,1,2,2,2,2,1000}
Returns: 7
{10,6,3,7,7,9,5,1,8,4}
Returns: 27
{3, 2, 2 }
Returns: 2
{3 }
Returns: 0
{10, 6, 3, 7, 7, 9, 5, 1, 8, 4 }
Returns: 27
{1, 1, 1, 1, 1, 1, 1 }
Returns: 3
{2, 1, 1 }
Returns: 1
{7, 9, 10 }
Returns: 9
{1, 2, 3 }
Returns: 2