Statistics

Problem Statement for "UsingStats"

Problem Statement

You have been given a int[] vals that is missing one element. Luckily, you know the mean of the entire set (with the missing element present). Using this information, reconstruct the full data set and return the median.

Definition

Class:
UsingStats
Method:
getMedian
Parameters:
int[], int
Returns:
int
Method signature:
int getMedian(int[] vals, int mean)
(be sure your method is public)

Notes

  • The mean of a list of values is their average. The median of a list of values is determined by sorting the list, and then taking the middle element.

Constraints

  • vals will contain between 2 and 50 elements, inclusive.
  • vals will contain an even number of elements.
  • Each element of vals will be between -1000 and 1000, inclusive.
  • mean will be between -1000 and 1000, inclusive.

Examples

  1. {50,100}

    75

    Returns: 75

    To have a 75 average, the missing value must be 75. This is also the median of the resulting set.

  2. {1,2,3,4}

    3

    Returns: 3

    The missing value is 5. The resulting median is 3.

  3. {0,0,100,100}

    61

    Returns: 100

    The missing value is 105, and the resulting median is 100.

  4. {889,820,404,-901}

    539

    Returns: 820

  5. {-211,-504,461,283,791,646,205,459,-186,516,-933,762,418,656,746,-368,888,270,-355,-207,-990,435,717,-338}

    845

    Returns: 418

  6. {-673,444,774,76,661,-605,260,579,-91,868,441,198,621,908,939,-763,-514,394,57,782,-957,64,-53,511,-168,61,234,-879,-114,-879,-875,-513,565,-35,51,-623,-742,36,553,-601,-289,311}

    301

    Returns: 61

  7. {-598,-389,878,412,694,-451,-720,-91,346,82,-19,-18,350,-537,806,-778,50,-677,-89,-962,-969,-372,681,91,349,-744,536,762,352,84,271,-397,534,-354,858,-784,682,-366,412,468,885,864,-568,-437,919,-375}

    -666

    Returns: 50

  8. {961,-199,-160,159,-364,-369,794,-490,-6,-485,-336,-729,-833,-122,-876,520,-815,-204,-97,-344,700,-176,377,-327,232,232,273,789,943,769}

    -723

    Returns: -160

  9. {912,-49,354,987,-551,876,155,-605,-971,639,996,169,147,-525,586,-644,557,-7,970,545,-822,60,899,37}

    296

    Returns: 169

  10. {281,952,-428,-161,-723,-776}

    -142

    Returns: -161

  11. {92,-866,-131,219,626,773,302,630,-285,692,235,-867,-78,-149,-746,-750,-398,-22,152,554,-789,420,801,-876,-201,689,240,-878,-12,-993,-982,805,-218,415,-701,227,-885,-781,413,987,141,-95,125,457}

    -991

    Returns: -12

  12. {-858,109,-941,295,189,-211,-740,-445,-723,-905,-994,82,-272,-614,-596,-420,992,-674,-370,67,322,-577,-970,629,-54,513,-742,-944}

    -439

    Returns: -445

  13. {-82,-710,-291,-424,-31,-440,-193,-983,-591,76,-454,175,-313,-743,-306,-504,696,-408,372,-972}

    -781

    Returns: -408

  14. {-624,-956,241,-483,202,-365,716,417,-864,35,998,-556,925,557,466,-479,-600,-729,644,-934,705,-122,-505,-531,738,547,534,-283,851,-30,-102,-642,918,289,-469,-215,449,-785}

    -635

    Returns: -102

  15. {592,-864,-550,994,609,-195,836,90,-335,858,-717,-925,618,-757,725,710,944,492,-77,814,835,-295,-328,563,112,74,-452,17,-357,-131,262,563,-384,852,410,-641,285,353,656,311,522,-636}

    172

    Returns: 285

  16. {-102,410,-422,-202,929,-735,-475,587,-443,-17,601,-788,-5,-717,266,-743,673,-227}

    191

    Returns: -102

  17. {-858,188,534,-185,-454,459,439,-170,539,155,399,-574,392,679,-72,971}

    -607

    Returns: 188

  18. {-497,738,-317,-71,688,-541,-855,-483,936,-708,-905,989,-315,670,-852,-741,66,906,910,-960,-223,862,859,-616,-506,539,172,-928,-409,991,-836,-801,-164,631,428,-263}

    272

    Returns: -223

  19. {-128,349,543,-564,367,268,-464,-737,953,-333,-568,354,-808,-384,-21,671,-383,-515,134,-220,-979,-598,-907,729,-244,-715,-841,-552,-832,861,922,310,872,-923,760,-186,-409,374,-182,-404,22,-623,307,-535}

    -646

    Returns: -244

  20. {-780,532,869,-822,-648,64,414,-167,935,-730,405,-202,89,843,-453,701,829,834,416,973,-595,-855,-260,-107,-597,799,-742,97,-792,36,874,967,181,-794,18,-24,238,484,8,948,-635,501,-367,989}

    -943

    Returns: 64

  21. {88,-204,697,-264,16,37,392,-976,401,-456,-20,-205,802,-846,-438,-233,604,240,189,909,-270,-821,148,835,632,315,640,214}

    -721

    Returns: 88

  22. {636,-170,-688,-491,139,-395,603,247,192,706,-987,-995,569,640,339,-130,293,162,132,-115,-135,-216,364,-618,-596,652,-235,-453,-199,-302,-935,-740,372,37}

    -659

    Returns: -130

  23. {566,878,-806,-686,782,390,595,217,543,376,-114,437,905,949,-356,-987,258,869,895,473,501,743,-472,-134,-70,-170,-366,70,116,875,12,20}

    581

    Returns: 376

  24. {75,929}

    -897

    Returns: 75

  25. {-8,-797}

    -857

    Returns: -797

  26. {-902,-700,-948,977,281,-518,498,-426,335,137}

    -182

    Returns: -426

  27. {-831,171,-71,-318,101,-222}

    -150

    Returns: -71

  28. {-507,-114,-126,466,468,-478,-982,472,602,379,-507,173,-934,-974,627,-371,-991,-680,-66,-356}

    -338

    Returns: -356

  29. {-188,35,51,-394,778,-395,106,121,19,-996,-617,-617,-390,391,710,-493,-239,532,-378,-624,-875,840,613,689,337,730,-26,674,769,-367,235,-577,-266,12,-863,611,312,833,358,225,365,46,335,851,-677,-119,919,-963}

    895

    Returns: 51

  30. {-223,343,989,799,-904,740,788,956,359,250,-782,-123,731,915,825,380,972,502,479,-424,-315,-283,-748,41,165,93}

    313

    Returns: 359

  31. {178,259,-230,488,-443,-226,975,-809,-297,390,58,738,761,247,-862,-39,554,-774,28,805,177,-474,-886,-386,582,354,-506,-955,456,-434,-467,211,201,60,-528,851,-41,811,-69,-289,-750,-519,-214,-321,33,-202,175,691,-228,53}

    190

    Returns: 28

  32. {432,-572,203,-608,-733,159,694,-323,-322,64,-16,-716,-490,-677,-717,-196,-284,66,173,-44,486,867,-258,449}

    661

    Returns: -44

  33. {407,567,-470,404,767,385,-40,515,654,-377,216,40,66,-732,792,920,-395,-675,107,676,-365,-715,-961,936,517,-727,887,-709,916,-792,615,909,-512,750,981,593,-971,365,-484,-778,-436,-626,-773,917,-231,527,-629,682}

    670

    Returns: 216

  34. {889, 820, 404, -901 }

    539

    Returns: 820

  35. {0, 0, 100, 100 }

    61

    Returns: 100

  36. {50, 100 }

    75

    Returns: 75

  37. {1, 2 }

    2

    Returns: 2

  38. {1, 4 }

    2

    Returns: 1

  39. {1, 2, 3, 4 }

    3

    Returns: 3

  40. {1, 3 }

    2

    Returns: 2

  41. {1, 2, 3, 4 }

    0

    Returns: 2

  42. {-90, 100, 1000, -15 }

    206

    Returns: 35

  43. {3, 1 }

    2

    Returns: 2

  44. {0, 1, 3, 4 }

    2

    Returns: 2

  45. {0, 1000 }

    500

    Returns: 500

  46. {-4, 7 }

    1

    Returns: 0

  47. {0, 2, 3, 4 }

    2

    Returns: 2

  48. {1, 2, 4, 5 }

    3

    Returns: 3

  49. {10, 10 }

    8

    Returns: 10


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: