← 返回 meta 的题目列表Second Largest Permutation From Given Multiset
类型:online_judge
Coding: Second Largest Permutation From a Given Multiset
Given an integer array nums (may contain duplicates), you may reorder elements arbitrarily to form all distinct permutations (compared lexicographically as sequences).
Return the second largest distinct permutation that can be formed from nums (strictly smaller than the largest permutation, but as large as possible).
If no second distinct permutation exists (e.g., all elements are equal), return the original array or an empty output (depending on the stated requirement).
Input
One integer n
One line of n integers nums[i]
Output
n integers: the second largest distinct permutation
Constraints (typical)
1 <= n <= 2 * 10^5
-1e9 <= nums[i] <= 1e9
Examples
[1,2,3,4,5] -> [5,4,3,1,2]
[1,1,5] -> [1,5,1]
Example
Input
5
1 2 3 4 5
Output
5 4 3 1 2