## Third Best Price

Time limit: 2 s

Memory limit: 64 MB

### Description

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 list of integers prices. The same price may occur multiple times in prices, but it should count only once in the ordering of available prices. See Example 2 for further clarification.

Find the third lowest price in this list. If there are less than three different car prices in prices, you should output -1.

### Input Format

The first line is an integer (1 <= <= 50), the number of prices.

The next line contains of N space-separated integers prices (1 <= prices[i] <= 1000).

### Output Format

An integer, the third lowest price, or -1 if there are less than three different car prices.

### Sample Input 1

```9
10 40 50 20 70 80 30 90 60
```

`30`

### Sample Input 2

```10
10 10 10 10 20 20 30 30 40 40
```

`30`

### Note

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

```1
10
```

`-1`

```5
80 90 80 90 80
```

`-1`

### Code Solution

```#include <stdio.h>
#include <stdlib.h>

int main()
{
int N,x,i,j,a = 0, pos = 0, temp;
int diff = 1, result = 0;
scanf("%d",&N);
if(N>=1 && N <= 50){
long int price[N];
while (a < N) {
if (scanf("%d", &x) != 1) break;
price[a] = x;
if (!(price[a] >=1 &&price[a] <=1000 )){
return 0;
}
a++;
}
for(i=0;i<N-1;i++){
pos=i;
for(j=i+1;j<N;j++){
if(price[j]<price[pos]){
pos=j;
}
}
if (pos!=i){
temp=price[pos];
price[pos]=price[i];
price[i]=temp;
}
}
for(i=0;i<N-1;i++){
if(price[i] != price[i+1]){
diff++;
if(diff == 3){
result = price[i+1];
}
}
}
if(diff<3){
printf("-1\n");
}
else{

printf("%d\n",result);
}

}
return 0;
}
```

Programming Problem Set #Third Best Price
Tagged on: