TopCoder problem "BuyingCheap" used in SRM 270 (Division II Level One)



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

0)
    
{10, 40, 50, 20, 70,
 80, 30, 90, 60}
Returns: 30
1)
    
{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.
2)
    
{10}
Returns: -1
3)
    
{80, 90, 80, 90, 80}
Returns: -1

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4754

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8067&pm=4754

Writer:

misof

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Sorting