[This page is auto-generated; please prefer to read the article’s PDF Form.]
Consider two permutations of input array a:
Say, Aim is the first element at which these two permutations differ, that is Aim≠Ajm. We assume that there is an order defined among elements of array a, and suppose we have Aim < Ajm under that order. Then we say that permutation P1 is Lexicographically smaller than P2. We will write P1 < P2.
For example, with array a = (1,2,3,4,5), the permutation (2,4,1,5,3) is smaller than (2,4,5,3,1).
Since elements of array a can be ordered, suppose we have their order as:
|
Then, we can reason that the following permutation is the smallest permutation in lexicographic ordering of all permutations:
|
Say, any other permutation differ from above permutation first at Aki, with another element d at that position. We know that Aki is the smallest of all elements to be placed at this position onwards. So, we must have Aki < d.
And with similar reasoning, following is the largest permutation:
|
Copyright © 2021 Nitin Verma. All rights reserved.