Imagine you have a sequence of positive integers representing a permutation, and you want to find the next smaller arrangement in lexicographical order - but with a twist! You can only make exactly one swap between two positions.
Given an array arr of positive integers (not necessarily distinct), your task is to return the lexicographically largest permutation that is smaller than the original array, achievable with exactly one swap operation.
If no such smaller permutation exists with a single swap, return the original array unchanged.
Example: For [3,2,1], we can swap positions 0 and 1 to get [2,3,1], which is lexicographically smaller. For [1,2,3], no single swap can make it smaller, so we return [1,2,3].
Note: Lexicographical order is like dictionary order - compare elements from left to right, and the first difference determines which is smaller.
Input & Output
Constraints
- 1 โค arr.length โค 104
- 1 โค arr[i] โค 104
- Array elements are positive integers
- Elements are not necessarily distinct