Algorithm
Permutation
Array Manipulation
Computer Science
Combinatorics

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.

Practice algorithms

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 nn distinct elements, it can be arranged in n!n! (n factorial) different ways.

For example, the set 1,2,3{1, 2, 3} has the following permutations:

1,2,3{1, 2, 3}1,3,2{1, 3, 2}2,1,3{2, 1, 3}2,3,1{2, 3, 1}3,1,2{3, 1, 2}3,2,1{3, 2, 1}

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:

  1. Identify the Rightmost Ascending Pair: Find the largest index kk such that the element at position kk is less than the element at position k+1k+1 (i.e., arr[k] < arr[k+1]). If no such position kk exists, then the array is sorted in descending order, and it is the last permutation.
  2. Find the Element to Swap: Find the largest index ll greater than kk such that arr[k] < arr[l].
  3. Swap Elements: Swap the elements at indices kk and ll.
  4. Reverse the Sequence: Reverse the sequence from position k+1k+1 to the end of the array. This step ensures that the sequence is in the smallest possible order after point kk.

Example

Consider the array 1,3,5,4,2{1, 3, 5, 4, 2}.

• The largest index kk such that arr[k] < arr[k+1] is k=1k = 1 (because 3 < 5). • The largest index ll such that arr[k] < arr[l] is l=3l = 3 (because 3 < 4). • Swap elements at indices 1 and 3 to get 1,4,5,3,2{1, 4, 5, 3, 2}. • Reverse the sequence from index 2 onward: 1,4,2,3,5{1, 4, 2, 3, 5}.

The next permutation is 1,4,2,3,5{1, 4, 2, 3, 5}.

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/StepExplanation
PermutationArrangement of elements in a sequence.
Lexicographical OrderDictionary-like ordering of sequences.
Nearest Permutation ProblemFinding the closest permutation based on criteria.
Identify Rightmost Ascending PairFind largest k where arr[k] < arr[k+1].
Find Swap ElementFind largest l > k such that arr[k] < arr[l].
Swap & ReverseSwap 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.