Next Permutation Problem

7:12 PM 5 Comments

Question 1.

It is an interesting exercise to write a program to print out all permutations of 1, 2, …, n. 
However, since there are 6227020800 permutations of 1, 2, …, 13, it is unlikely that we would ever run this program on an input of size more than 10.
However, here is another interesting problem whose solution can also be used to generate permutations. We can order the permutations of 1, 2, …, n under the lexicographic (or dictionary) order. Here are the permutations of 1,2,3 in lexicographic order:
1 2 3    1 3 2    2 1 3    2 3 1    3 1 2    3 2 1

The problem we have is the following: given a permutation of 1,2, …, n, generate the next permutation in lexicographic order. 
For example, for   2 3 1 4   the answer is   2 3 4 1.

Input format
The first line of the input contains two integers, N and K. This is followed by K lines, each of which contains one permutation of 1, 2,…,N.

Output format
The output should consist of K lines. Lines i should contain the lexicographically next permutation corresponding to the permutation on line i+1 in the input.

Test data
You may assume that 1 ≤ N≤ 1000 and K≤10.

Example

We now illustrate the input and output formats using the example described above.

Sample input
3 2
3 1 2
2 3 1
Sample output
3 2 1
3 1 2





Hint and Answers ! :- http://programxperience.blogspot.in/p/answer-1.html

Unknown

Live as if you were to die tomorrow. Learn as if you were to live forever.

5 comments:

  1. Where to start think in right direction /how to start

    ReplyDelete
    Replies
    1. Firstly, you have to read question properly 3-4 times
      Then you get some idea
      About what u have to do
      Then start doing it manually on copy not on computer
      By doing this your thoughts maximize and more idea strikes
      But if you face first time that type of program don't panic we are here
      U have to devote some time to it

      Delete
  2. i need a coding for this program any one can

    ReplyDelete