Problem 201
Second Smallest Element
Easy · 10 pts · Array
Problem
You are given an array of N integers. Print the second smallest distinct value: the smallest value that is strictly greater than the minimum. Repeated copies of the minimum do not count, so in 1 1 2 the answer is 2. If all elements are equal, there is no second smallest distinct value, so print -1. One pass is enough: track the smallest value and the smallest value strictly above it, updating both as you go.
Input
First line: integer N. Second line: N space-separated integers.
Output
Print the second smallest distinct value, or -1.
Constraints
1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000
Example input
4 5 1 3 1
Example output
3
Explanation
In 5 1 3 1, the distinct values are 1, 3 and 5. The smallest is 1, and the next one up is 3, so the answer is 3.