Nearest permutation to given array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Permutations are fundamental in the field of combinatorics and have significant applications in computer science, mathematics, and related disciplines. Finding the "nearest permutation" of a given array involves deriving a permutation that satisfies a certain condition, such as being lexicographically closest or differing by the smallest possible number of swaps. This article delves into the concept of permutations, discusses how to determine the nearest permutation given a specific array, and provides relevant examples and technical details to elucidate this topic.
Understanding Permutations
Permutations refer to the various ways in which elements of a collection can be arranged in sequences or orders. If you have an array with distinct elements, it can be arranged in (n factorial) different ways.
For example, the set has the following permutations:
• • • • • •
Lexicographical Order and Nearest Permutation
One common way to order permutations is lexicographically, much like how words are arranged in a dictionary. The nearest permutation problem often involves finding the next or previous permutation in this lexicographical order.
Algorithm to Find the Next Lexicographical Permutation
To find the nearest permutation that is larger than an existing permutation, one can use the following strategy:
- Identify the Rightmost Ascending Pair: Find the largest index such that the element at position is less than the element at position (i.e., arr[k] < arr[k+1]). If no such position exists, then the array is sorted in descending order, and it is the last permutation.
- Find the Element to Swap: Find the largest index greater than such that arr[k] < arr[l].
- Swap Elements: Swap the elements at indices and .
- Reverse the Sequence: Reverse the sequence from position to the end of the array. This step ensures that the sequence is in the smallest possible order after point .
Example
Consider the array .
• The largest index such that arr[k] < arr[k+1] is (because 3 < 5). • The largest index such that arr[k] < arr[l] is (because 3 < 4). • Swap elements at indices 1 and 3 to get . • Reverse the sequence from index 2 onward: .
The next permutation is .
Applications
Understanding permutations and the nearest permutation problem is beneficial for solving various computational tasks, such as:
• Sorting Algorithms: Permutations are core components in designing and analyzing sorting algorithms. • Combinatorial Optimization: Problems like the traveling salesman problem rely on permutations to explore possible solutions. • Cryptography: Permutations are integral in the design of cryptographic algorithms and protocols.
Summary Table
| Concept/Step | Explanation |
| Permutation | Arrangement of elements in a sequence. |
| Lexicographical Order | Dictionary-like ordering of sequences. |
| Nearest Permutation Problem | Finding the closest permutation based on criteria. |
| Identify Rightmost Ascending Pair | Find largest k where arr[k] < arr[k+1]. |
| Find Swap Element | Find largest l > k such that arr[k] < arr[l]. |
| Swap & Reverse | Swap at indices k and l, then reverse from k + 1 onward. |
Conclusion
The concept of permutations and finding the nearest permutation plays a vital role in many computational and mathematical problems. By utilizing efficient algorithms, one can explore permutations to solve complex tasks more effectively. Understanding and implementing the correct permutation algorithms can lead to significant improvements in problem-solving techniques across various domains.
Related reading
- Need algorithm suggestions for flight routings
- Need an algorithm to split a series of numbers
- Need assistance with algorithm to find the maximum path in a DAG
- Need Better Algorithm for Finding Mapping Between 2 Sets of Points with Minimum Distance
- Need a data set for fraud detection
- need some clarifications about dispatch queue, thread and NSRunLoop
- Need to devise a number crunching algorithm
- Nesting maximum amount of shapes on a surface

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.