Previous Permutation With One Swap - Problem

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

example_1.py โ€” Basic Case
$ Input: [3,2,1]
โ€บ Output: [2,3,1]
๐Ÿ’ก Note: We can swap positions 0 and 1 to get [2,3,1], which is lexicographically smaller than [3,2,1] and is the largest such permutation possible with one swap.
example_2.py โ€” No Valid Swap
$ Input: [1,2,3]
โ€บ Output: [1,2,3]
๐Ÿ’ก Note: The array is already in ascending order, so no single swap can make it lexicographically smaller. Return the original array.
example_3.py โ€” Duplicates
$ Input: [3,1,1,3]
โ€บ Output: [1,3,1,3]
๐Ÿ’ก Note: We can swap positions 0 and 1 to get [1,3,1,3]. Even though there are duplicates, this gives us the optimal result.

Constraints

  • 1 โ‰ค arr.length โ‰ค 104
  • 1 โ‰ค arr[i] โ‰ค 104
  • Array elements are positive integers
  • Elements are not necessarily distinct

Visualization

Tap to expand
Previous Permutation StrategyExample: [3, 2, 1] โ†’ [2, 3, 1]Original:321Step 1: Check i=0321Found: 2 < 3 (choose 2 as largest)Step 2: Swap positions 0,1231Result:231โœ“ Lexicographically smaller than [3,2,1]Key insight: Start from the right and find the first position where you can place a smaller digitThen choose the largest such smaller digit to maximize the result
Understanding the Visualization
1
Scan from Right
Start from the rightmost position and move left to find the first position where you can make a beneficial change
2
Find Best Candidate
For that position, look to the right and find the largest digit that is still smaller than the current digit
3
Make the Swap
Swap these two positions to get the lexicographically largest permutation that is smaller than the original
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because we want the rightmost position where we can make a beneficial change, and among all candidates, we choose the largest smaller digit to maximize our result.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen