Question: Prolog program Write programs to generate the next higher permutation of a list of positive integers, in two ways: By applying the following definition of
Prolog program

Write programs to generate the next higher permutation of a list of positive integers, in two ways: By applying the following definition of the problem: Permutation LI is the next, higher permutation of L if L1 is a higher permutation of L and LI is not far higher than L. L1 is a higher permutation of L if it is a permutation of L and is higher than L. LI is higher than L if there is a leftmost element of LI that is greater than the leftmost element of L. (below, L1 higher than L in both pairs). (e.g. L = [4, 2, 5, 3]; L1 = [5, 2, 4, 3] L = [5, 2, 3, 4];L1 = [5, 2, 4, 3]. L1 is far higher than L if there is a higher permutation L2 of L such that LI is higher than L2. By implementing the following algorithm: Scan L from the right end. Find the rightmost number (R) that is less than it's right neighbour (R.n) (e.g. L = [1, 2, 3, 5, 4, 2] R = 3 Scan L from the right again finding the rightmost number X > R (X = 4. Swap X and R ([1, 2, 4, 5, 3, 2], note numbers after 4 in decreasing order) and then reverse the digits on the right of X ([1, 2, 4, 2, 3, 5]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
