Statistics

Problem Statement for "BuyingCheap"

Problem Statement

Steve would like to buy a new car. He isn't wealthy, so he would prefer a reasonably cheap car. The only problem is that the quality of the cheapest cars is... let's say questionable.

Thus Steve decided to make a list of car prices and to buy a car with the third lowest price.

You will be given a int[] prices. The same price may occur multiple times in prices, but it should count only once in the ordering of available prices. See Example 1 for further clarification.

Write a function that returns the third lowest price in this list. If there are less than three different car prices in prices, your method should return -1.

Definition

Class:
BuyingCheap
Method:
thirdBestPrice
Parameters:
int[]
Returns:
int
Method signature:
int thirdBestPrice(int[] prices)
(be sure your method is public)

Constraints

  • prices contains between 1 and 50 elements, inclusive.
  • Each element in prices will be between 1 and 1000, inclusive.

Examples

  1. {10, 40, 50, 20, 70, 80, 30, 90, 60}

    Returns: 30

  2. {10, 10, 10, 10, 20, 20, 30, 30, 40, 40}

    Returns: 30

    The lowest price is 10, the second lowest is 20 and the third lowest is 30.

  3. {10}

    Returns: -1

  4. {80, 90, 80, 90, 80}

    Returns: -1

  5. {1000}

    Returns: -1

  6. {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

    Returns: -1

  7. {1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

    Returns: -1

  8. {1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

    Returns: -1

  9. {1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,3,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

    Returns: 3

  10. {1,1,1,1,1,1,1,1000,1,1,1,1,1,2,1,1,1,999,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

    Returns: 999

  11. {1,2,3}

    Returns: 3

  12. {1,3,2}

    Returns: 3

  13. {2,1,3}

    Returns: 3

  14. {2,3,1}

    Returns: 3

  15. {3,1,2}

    Returns: 3

  16. {3,2,1}

    Returns: 3

  17. {1,1}

    Returns: -1

  18. {89,98}

    Returns: -1

  19. {997,998,997,998,997,1000,997,997,999,998}

    Returns: 999

  20. {11,12,13,14,15,16,17,18,19,20,911,912,913,914,11,12,13,14,15,16,17,18,19,20,911,912,913,914,7,11,12,13,14,15,16,17,18,19,20,911,912,913,914}

    Returns: 12

  21. {384,887,778,916,794,336,387,493,650,422,363,28,691,60,764,927,541,427,173,737,212,369,568,430,783,531,863,124,68,136,930,803,23,59,70,168,394,457,12,43,230,374,422,920,785,538,199,325,316,371}

    Returns: 28

  22. {414,527,92,981,957,874,863,171,997,282,306,926,85,328,337,506,847,730,314,858,125,896,583,546,815,368,435,365,44,751,88,809,277,179,789,585,404,652,755,400,933,61,677,369,740,13,227,587,95,540}

    Returns: 61

  23. {796,571,435,379,468,602,98,903,318,493,653,757,302,281,287,442,866,690,445,620,441,730,32,118,98,772,482,676,710,928,568,857,498,354,587,966,307,684,220,625,529,872,733,830,504,20,271,369,709,716}

    Returns: 98

  24. {341,150,797,724,619,246,847,452,922,556,380,489,765,229,842,351,194,501,35,765,125,915,988,857,744,492,228,366,860,937,433,552,438,229,276,408,475,122,859,396,30,238,236,794,819,429,144,12,929,530}

    Returns: 35

  25. {777,405,444,764,614,539,607,841,905,819,129,689,370,918,918,997,325,744,471,184,491,500,773,726,645,591,506,140,955,787,670,83,543,465,198,508,356,805,349,612,623,829,300,344,747,569,341,423,312,811}

    Returns: 140

  26. {606,802,662,731,879,306,321,737,445,627,523,466,709,417,283,259,925,638,63,625,601,37,453,900,380,551,469,72,974,132,882,931,934,895,661,164,200,982,900,997,960,774,814,669,191,96,927,467,85,341}

    Returns: 72

  27. {91,685,377,543,937,108,446,757,180,419,888,413,349,173,660,10,337,211,343,588,207,302,714,373,322,256,820,600,722,905,940,812,941,668,706,229,128,151,985,659,921,225,423,270,397,82,631,85,293,973}

    Returns: 85

  28. {673,851,626,386,223,300,641,43,899,714,299,191,525,591,210,582,820,337,733,156,995,5,380,770,274,777,851,256,861,143,580,885,994,206,622,568,505,614,962,755,327,260,945,203,203,507,785,22,843,869}

    Returns: 43

  29. {529,190,873,909,959,499,37,809,754,249,304,334,134,649,891,755,568,747,369,530,501,47,789,798,250,991,304,34,364,498,254,893,687,126,153,997,976,189,158,730,437,461,415,922,461,305,29,28,51,749}

    Returns: 34

  30. {557,903,795,698,700,44,40,3,429,404,501,682,648,539,160,152,536,135,340,693,216,128,505,630,50,965,286,430,344,336,178,901,239,972,950,290,368,989,293,796,744,145,830,391,683,341,542,570,827,233}

    Returns: 44

  31. {262,43,361,118,24,762,82,310,191,426,997,368,678,235,691,627,525,58,615,169,206,359,313,387,101,347,727,995,917,553,579,530,947,291,648,971,52,81,632,594,858,628,313,887,215,356,513,91,413,480}

    Returns: 52

  32. {611,970,190,275,356,642,621,434,988,889,339,567,771,285,857,418,607,261,850,238,206,60,218,519,946,784,874,459,874,638,290,484,608,479,758,315,472,730,101,460,619,439,26,389,75,234,158,682,494,359}

    Returns: 75

  33. {271,700,418,840,570,364,623,795,174,848,432,463,683,391,293,792,58,116,522,158,575,492,948,952,232,22,538,741,55,31,99,326,82,517,517,3,232,140,797,405,339,581,219,22,971,863,813,380,978,686}

    Returns: 31

  34. {537,905,177,484,208,760,858,745,500,912,128,951,237,561,819,106,564,50,245,712,806,935,292,376,956,615,590,769,994,919,806,883,823,983,718,31,94,575,127,594,487,254,544,75,815,714,180,378,763,776}

    Returns: 75

  35. {89,920,711,733,295,18,347,236,138,692,154,944,574,329,926,292,711,19,218,837,964,56,91,859,131,905,572,662,634,686,790,74,605,852,806,251,869,504,486,7,196,640,950,121,968,227,764,678,597,982}

    Returns: 19

  36. {866,561,37,956,771,519,212,343,533,197,380,322,271,985,173,428,235,41,284,73,399,831,64,348,951,31,574,715,60,523,48,925,83,436,233,205,955,444,899,487,641,279,160,263,263,684,42,849,724,325}

    Returns: 41

  37. {273,123,155,336,822,458,366,748,172,777,270,219,702,704,654,934,908,960,729,807,798,721,85,309,335,699,992,377,899,716,53,172,190,560,507,11,17,225,110,540,1,379,110,54,82,115,339,990,427,68}

    Returns: 17

  38. {148,224,788,232,533,123,282,876,851,180,591,255,351,132,814,858,495,182,82,604,721,434,983,182,488,416,297,826,405,723,893,552,298,33,135,182,507,416,58,709,596,1000,963,298,484,777,155,978,310,588}

    Returns: 82

  39. {933,383,22,267,564,861,683,212,686,87,286,931,991,584,315,477,117,821,893,526,529,840,526,491,137,361,619,644,338,929,583,622,311,956,889,226,816,571,438,854,9,723,784,351,658,98,828,127,270,72}

    Returns: 72

  40. {652,150,911,529,640,399,889,611,394,578,891,977,200,553,932,88,778,100,658,567,953,18,642,736,369,299,185,196,777,806,267,429,955,529,309,594,279,198,556,673,775,446,1,326,998,284,413,128,383,422}

    Returns: 88

  41. {694,335,440,335,422,160,986,958,355,762,763,973,542,717,853,851,663,483,400,218,155,174,16,507,852,365,791,264,492,173,38,538,860,829,872,281,988,857,591,342,971,353,666,512,70,518,362,84,352,113}

    Returns: 70

  42. {301,507,639,668,365,490,33,155,105,876,680,142,413,539,970,637,171,957,845,761,650,815,466,315,327,887,184,40,970,536,153,622,394,791,290,110,632,674,265,736,549,296,878,314,834,199,950,356,156,794}

    Returns: 105

  43. {469,157,961,934,824,287,172,359,678,141,246,182,762,991,324,51,101,955,76,365,43,625,660,920,290,845,470,239,552,977,384,20,134,344,305,957,982,476,667,12,968,913,193,730,903,869,132,3,175,208}

    Returns: 20

  44. {719,217,184,378,488,473,574,958,63,126,934,798,497,419,142,154,727,475,981,394,486,949,306,31,30,560,899,161,563,425,720,281,642,903,11,481,727,584,790,141,709,724,939,558,494,432,711,221,906,691}

    Returns: 31

  45. {614,392,639,271,422,668,830,672,181,744,96,900,25,89,155,387,570,233,970,711,374,31,434,664,588,280,95,650,500,352,340,465,743,331,87,516,350,916,187,882,12,635,134,388,723,288,774,644,520,743}

    Returns: 31

  46. {355,245,125,140,260,64,419,354,713,270,706,405,734,800,735,820,316,436,88,854,670,451,488,803,838,563,90,611,206,961,705,912,558,830,404,817,893,822,523,606,444,580,362,529,379,448,701,46,883,788}

    Returns: 88

  47. {900,552,590,387,354,427,949,795,37,507,108,93,418,665,922,821,481,167,995,355,124,438,934,485,318,313,932,18,710,166,157,609,70,746,996,423,172,296,570,560,802,677,653,572,341,926,744,173,92,90}

    Returns: 70

  48. {528,567,879,813,51,197,125,334,214,187,500,371,795,569,116,142,343,640,438,264,199,591,940,203,514,632,129,258,805,572,347,684,139,226,496,541,422,973,227,635,159,726,357,953,646,473,447,340,112,884}

    Returns: 116

  49. {604,662,826,543,217,340,175,345,597,331,268,295,14,758,520,861,651,293,833,877,280,991,954,636,296,599,108,742,938,571,977,541,585,802,84,801,493,610,497,441,940,764,736,305,874,607,165,524,252,350}

    Returns: 108

  50. {752,531,340,705,166,987,303,626,80,592,548,408,485,132,562,920,932,54,529,428,495,820,544,582,124,769,188,640,644,439,989,395,321,681,99,487,19,753,464,99,696,11,506,180,143,67,99,426,473,979}

    Returns: 54

  51. {998,999,1000}

    Returns: 1000

  52. {999,1000}

    Returns: -1

  53. {999,999,1000}

    Returns: -1

  54. {1000,1000,1000}

    Returns: -1

  55. {10 }

    Returns: -1

  56. {10, 40, 50, 20, 70, 80, 30, 90, 60 }

    Returns: 30

  57. {10, 10, 10, 10, 20, 20, 30, 30, 40, 40 }

    Returns: 30

  58. {1, 2, 3 }

    Returns: 3

  59. {30, 30, 30, 40, 40, 40 }

    Returns: -1

  60. {3, 2, 1 }

    Returns: 3

  61. {30, 20, 10 }

    Returns: 30

  62. {20, 30, 40 }

    Returns: 40

  63. {1, 1, 2 }

    Returns: -1

  64. {10, 10, 10, 10, 70, 80, 10, 10, 10 }

    Returns: 80

  65. {11, 12, 13 }

    Returns: 13


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: