#K12521. Second Smallest Unique Number
Second Smallest Unique Number
Second Smallest Unique Number
Given an array of integers, your task is to find the second smallest unique number in the array. If there is no such number because the array has fewer than two distinct numbers, print -1.
The input is provided via standard input (stdin
) and the output should be printed to standard output (stdout
).
Note: The array elements might include negative numbers, and duplicate values should be considered only once when determining uniqueness.
Mathematical Formulation: Let \( S \) be the set of unique elements from the array \( A = [a_1, a_2, \dots, a_n] \). If \(|S| < 2\), output \(-1\); otherwise, if \( S = \{s_1, s_2, \dots\} \) with \( s_1 < s_2 < \dots \), output \( s_2 \).
inputFormat
The first line of input contains an integer \( n \) representing the number of elements in the array. The second line contains \( n \) space-separated integers.
Example:
5 4 2 1 3 5
outputFormat
Output a single integer: the second smallest unique number in the array. If such a number does not exist, output -1.
## sample5
4 2 1 3 5
2