TopcoderArchiveVisit Topcoder
SRM3 Nov 2005
SRM 270

BuyingCheap

Problem statement, definition, constraints, and public examples.

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
Back to all problems