Statistics

Problem Statement for "MedianOfThree"

Problem Statement

The median of a collection of numbers is the number that is ranked in the middle after the collection is sorted. If the size of the collection is even, then the median is the average of the two middle numbers.

For example:

  • median of { 1, 2, 3, 4, 5 } is 3
  • median of { 3, 9, 55, 7, 12, 14, 10 } is 10
  • median of { 2, 8, 4, 6 } is 5

A 3-number combination is formed by picking any 3 numbers out of a collection. The order is unimportant. Hence, { 1, 2, 3 }, { 2, 1, 3 }, and { 3, 1, 2 } represent the same 3-number combination.

Each number in the collection is considered distinct, even if their values are equal.

For example, { 1, 2, 2, 3 } has four distinct numbers even though two of them are equal to 2.

It may be easier to label them as { a, b, c, d }, with a = 1, b = 2, c = 2, and d = 3. We can then enumerate all the possible 3-number combinations as: { a, b, c }, { a, b, d }, { a, c, d }, { b, c, d }. They are { 1, 2, 2 }, { 1, 2, 3 }, { 1, 2, 3 }, and { 2, 2, 3 }, respectively.

You are given a int[], numbers. Your task is to write a function that calculates the median of each 3-number combination of numbers and returns the sum of these medians.

For example:

{ 1, 2, 3, 4 } has the following 3-number combinations: { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 }, and { 2, 3, 4 }. Their medians are 2, 2, 3, and 3, respectively. The sum of these medians is 2 + 2 + 3 + 3 = 10.

{ 1, 2, 2, 3 } has the following 3-number combinations: { 1, 2, 2 }, { 1, 2, 3 }, { 1, 2, 3 }, and { 2, 2, 3 }. Their medians are 2, 2, 2, and 2, respectively. The sum of these medians is 2 + 2 + 2 + 2 = 8.

Definition

Class:
MedianOfThree
Method:
sum
Parameters:
int[]
Returns:
int
Method signature:
int sum(int[] numbers)
(be sure your method is public)

Notes

  • A number may appear more than once in numbers

Constraints

  • numbers contains between 3 and 50 elements, inclusive
  • Each element of numbers is between -1,000 and 1,000, inclusive

Examples

  1. {1,2,3,4}

    Returns: 10

    This is the same as the example above.

  2. {1,2,3,4,5}

    Returns: 30

    There are ten 3-number combinations and the sum of their medians is 30.

  3. {-3,2,-1,0,1,-2,3}

    Returns: 0

  4. {1,5,2,4,3,3,4,2,5,1}

    Returns: 360

  5. {0,0,0,0,0,0,0,0,0,0}

    Returns: 0

  6. {-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: -19600000

  7. {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: 19600000

  8. {183,858,75,-774,552,-772,496,547,377,371,768,576,438,-262,275,-505,-934,584,-322,-174,884,-433,-332,-421,343,478,-714,-849,521,506,-96,637,-217,418,-734,-863,-311,642,-388}

    Returns: 603073

  9. {240,745,617,454,714,92,-83,-915,-722,-355,-335,352,-962,696,334,-301,-416,318,-545,289,-928,-171,821,583,776,460,-536}

    Returns: 237648

  10. {688,632,129,151,-622,307,-571,730,-195,-890,-690,641,-699,-224,-901,816,-121,41,-311}

    Returns: -62833

  11. {-846,87,60,-381,998,444,-701,-16,885,358,443,-967,742,331,84,536,-52,-104,-13,-362,391,-329,317,-118,442,40,-506}

    Returns: 239536

  12. {824,794,352,-911,438,-985,-194,594,320,890,538,-642,-803,-319,-126,110,595,-177,-226,597,534,-20,-870,228,853,-35,656,830,-903,-737,-478,517,-139,-343,-674,377,788,-65,98,705,633,-238,-546,640,-967,341,724}

    Returns: 1850687

  13. {167,450,781,-115,444,-125,-288,873,-773,-359,354,-904,944,943,-495,312,-618,829,805,593,-162,952,-255,433,894,253,651,-17,331,732,58,-859,367,-921,771,861,60,210,-13,844,-814,-393,-284,445,-237}

    Returns: 2902934

  14. {-699,396,-243,852,326,312}

    Returns: 4440

  15. {167,-124,516,-374,-781,-932,-16,864,855,614,-559,-305,482,-269,-331,-978,-271,321,75,-799,716,-351,866,225,899}

    Returns: 49400

  16. {59,-321,-13,267,-80}

    Returns: -115

  17. {-707,198,321,376,231,-909,-127,522,298,892,761,400,-642}

    Returns: 53981

  18. {-866,833,422,245,784,-998,903,14,-858,337,-948,-979,-647,-582,206,-62,-437,418,474,-903,-559,430,-316,-557,-221,863,-893,-874,260,234,-407,-22,-787,-285,-928}

    Returns: -1455907

  19. {60,-823,91,-74,-300,159,-975,-206,-192,560,-224,-706,-258,907,-401,-47,-389}

    Returns: -125540

  20. {-143,-20,-525,740,43,182,911,-972,114,-747,-124,402,292,-980,117,735,-587,347,-62,92,-231,-733,916,157,-561,-130,-241,901,802,-313,-953,480,476,995,-257,182,125,-389,117,502}

    Returns: 478890

  21. {84,-707,215,-632,566,189,-330,-911,-400,-423,730,-202,-666,-366,-818,-264,-208,-663,-148,834,210,-85,835,775,186,-155,819,10,-204,189,954,240,-392,702,-938,-899,-723,-478}

    Returns: -905704

  22. {186,128,-976,-816,943,-616,-86,-339}

    Returns: -13760

  23. {-648,-528,800,-409,986,-960,-601,-185,-61,-844,-42,873,-176,-366,-466,642,-587,-476,-633,644,58,304,-683,837,531,218,620,-298,110,-852,809,311,-129,-187,-698,302,-741,216,467,-146,-804,-632,-409,571,-399,-218}

    Returns: -1774668

  24. {731,-39,247,-869,81,-651,381,990,-803,941,285,-270,926,615,-652,-900,-564,731,-39,172,-926,-346,-440,-536,-747,362,-137,422,-400,-416,-829,-874,-516,-39}

    Returns: -955522

  25. {82,-19,75,581,214,-640,661,415,526,441,-827,-766,899,500,861,409,-692,-611,358,627,6,-930,-811,899,190,-655,965,812,-561,349,-961,-744,731,-973,271,-180,132,879}

    Returns: 842550

  26. {-66,80,-590,-998,-965,979,766,796,-100,-388,-180,966,861,322,-326,-831}

    Returns: 9946

  27. {721,-658,581,212,-337,-1,-896,171,-635,0,-180,123,-976,331,-154,-852,-273,133,-992,-393,706,-285}

    Returns: -253296

  28. {-485,-83,-18,-730,-856,125,9,-454,938,-695,710,-88,-907,935,251,936,-611,485,634,81,-717,-389,384,-105,593,162,-762,421,429,479,-877,-240,221,-644,373,736}

    Returns: 91030

  29. {186,-797,569,-55,288,782,-74,762,-962,-956,-565,664,-231,954,891,-756,836,-428,126,183,610,-762,-167,673,-494,-449,524,-839,-271,565,-226,799,958,-425,-399,-505,-299,-630,-776,274,618,-539,853,674,366,606}

    Returns: 823978

  30. {962,-879,481,-400,-725,514,-671,-710,394,-527,-868,751,-813,199,-250,52,-966,843,-65,-755,-112,-258,293,-132,-416,-579,309,234,-585,152,272,851}

    Returns: -635524

  31. {697,-650,-896,761,-932,-774,541,-883,563,238,-46,178,595,169,682,336,712,344,399,581,-173,-290,-330,-386,338,-929,516,989,489,-916,-499,-1000,-918,-418,-26,-367,-891,191}

    Returns: -280796

  32. {-360,-894,-398,-506,-185,-990,288,-549,-281,-787,-464,425,-516,677,-743,712,30,724,-122,510,666}

    Returns: -210447

  33. {-498,539,-227,-905,-761,-203}

    Returns: -8206

  34. {267,525,585,675,994,818,-935,-89,466,241,-746,-916,889,-753,-461,555,655,-223,-625,465,-377,-819,500,-639,114,-541,-297,639,-142,852,-166,927,-251,-91,-673,-193,-701,940,296,26,-61,631}

    Returns: 721424

  35. {360,-876,-504,-915,466,-709,219,225,311,-238,166,-919,-360,-249,84,-478,127}

    Returns: -113468

  36. {262,-786,513,-971,-907,232,-167,-384}

    Returns: -16022

  37. {600,448,980,150,-37,325,-879}

    Returns: 10524

  38. {154,-505,575,-423,566,646,-182,-994,282,590,411,719,-106,-973,231,725,-896,-498,-674,675,-819,-698}

    Returns: -45212

  39. {846,-856,-2,-628,713,520,-571,88,470,766,-857,513,601,548,286,857,-602,85,820,40,746,-614,597,-638,61,704,986,184,83,-986,646,221,879,-344,480,-260,275,453,6,-109,365,320,871,-985,154,-324,648,445}

    Returns: 3964654

  40. {-549,464,945,-168,-404,-455,-438,-64,660,912,-659,-659,-748,206,-60,-431,-268,-317,-21,-534,-228,-430,-51,-122,-697,-647,918,-270,-57,-853,519,-666,578,-380,645,259,-973,-132,-199,259,-699}

    Returns: -2025065

  41. {-43,-901,597,824,-348,-390,620,81,893,601,-292,322,-772,20,38,-796}

    Returns: 21792

  42. {344,791,-690,-706,-947,769,597,927,235,-964,452,-334,-443,-37,640,433,-347,746,517,831,354,-102,673,-349,-233,843,-111,167,-708,-768,-83,866,824}

    Returns: 900471

  43. {575,213,55}

    Returns: 213

  44. {292,111,-946,-128,817,809,-978,976,-104,-32,-121,-658,304,25}

    Returns: 13196

  45. {614,-1000,235,-781,96,-31,893,939,-771,-670,-218,215,4,748,-882}

    Returns: -20049

  46. {-304,-641,833,703,69,86,813,-480,838,905,136,56,719,-796,-144,-391,256,-366,-881,-979,148,659,-184,916,-355,-971}

    Returns: 77154

  47. {456,-759,780,78,-470,-696,920,338,-195,-93,800,-414,430,143,652,101,295,89,-780,-480,749,-462,642,-787,862,778,-740,420,-149,-790,-227,-941,180,-334,-620,-191,-325,-917}

    Returns: -471910

  48. {248,755,-741,163,-370,892,760,-910,850,-11,-176,-543,-194}

    Returns: 16811

  49. {-495,-1000,-15,976,-237,-898,-189,42,465,908,737,-929,-629,625,829,659,70,-803,438,-899,652,-25,-899,-357,-297,62,-789,531,378,455,-924,990,647,280,-746,437,572,992,42}

    Returns: 616420

  50. {213,-842,-852,863,61,486,383,760,189,-459,760,939,421,50,-715,644,690,-93,-685,124,296,654,861,218,171,614,514,679,-436,-896,151,464,-284,-814,-718,626,-207,-754,924,-59,-352,255,-607,887,625,-616,298,822}

    Returns: 2919104

  51. {-662,233,-289,-558,764,-596,-313,610,562,-637,746,323,-143,795,586,-260,-415,368,168,309,919}

    Returns: 172156

  52. {956,550,59,6,834,-979,672,-949,-172,955,-318,182,-201,565,280,-636,-778,659,-143,359,-930,-183,811,216,-349,510,-993,152,967,-444,-863,201,-412,-911,63,190,964,673,748,-281}

    Returns: 687542

  53. {852,386,814,211,-946,901,115,408,-625,-10,101,-989,-370,-98,915,203,89,210,-250,-763,-688,569,284,-377}

    Returns: 119514

  54. {-208,148,-828,-290,-467,870,103,38,783,184,367,100,200,220,-477,974,-546,-836,149,141,-317,877,-833,336,97,191,-184,-99,-94,-937,430,492,-399}

    Returns: 66610

  55. {822,-94,772,159,981,-424,102,-991,-393,411,-427,-17,-86,-640,771,-459,-312,44,252,536,325,734,213,73,809,-165,-681}

    Returns: 254212

  56. {844,-902,-566,-743,-439,-145,-744,-6,176,-19,-26,-658,-396,-307,-329,-188}

    Returns: -174198

  57. {-480,242,-741,-807,868,795,-744,911,-110,361,832,305,-235,994,-494,286,-74,139,883,-335,-480,-650,397,-768,-319,54,318,-195,-160,-926,329,929,-762,525,-562,-814}

    Returns: -235214

  58. {999,998,-997}

    Returns: 998

  59. { 1, 2, 3 }

    Returns: 2

  60. { 1, 5, 2, 4, 3, 3, 4, 2, 5, 1 }

    Returns: 360

  61. { -1, 2, -3, 2, 3 }

    Returns: 11

  62. { 1, 3, 5, 6, 7, 8, 9 }

    Returns: 205

  63. { 4, 1, 2, 3, 5, 6, 7 }

    Returns: 140

  64. { 2, 1, 3 }

    Returns: 2

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

    Returns: 660

  66. { 3, 10, -1, 4, 10, 11, 12 }

    Returns: 272

  67. { 1, 5, 5 }

    Returns: 5

  68. { 3, 2, 1 }

    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: