Code to find Permutation of Numbers

Introduction

The article is about writing a Code to find permutation of numbers, the program prints all possible permutations of a given list or array of numbers. I have this question listed on my Stackoverflow.com account as well. To know the opinions of other people on the same and to find out other ways to solve the problem, please visit the link

Also, you might be interested in the code for Combinations. Find it here

Problem Statement

Given a list of numbers, print the possible permutations. The program must also be able to print P(n,r) where N and r can be input parameters.
Before writing the code it would be better to have a clear understanding of what permutation means.

P(n.r) means given n items choose r at a time and list all possible combination of r items from the set of n items.

For e.g. if there are three numbers 1, 2 and 3 then P(3,2) would result into :
P(3,2) : Count (6) :- [[3, 1], [2, 1], [3, 2], [1, 3], [2, 3], [1, 2]]

Code and Implementation

Below is the java code to find permutation of numbers

Conclusion

Here is the output when we try to run the program

P(3,3) : Count (6) :- [[1, 2, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2], [2, 1, 3], [1, 3, 2]]
P(3,2) : Count (6) :- [[3, 1], [2, 1], [3, 2], [1, 3], [2, 3], [1, 2]]
P(3,1) : Count (3) :- [[3], [1], [2]]
P(3,0) : Count (1) :- [[]]

Stay connected and stay Subscribed

  • hariny

    This implementation is to calculate the possible permutation without repetitions. Say I want all the possible permutations of P(3,3), it should also display [[1,1,1], [2,2,2],[3,3,3],[1,2,2],[1,2,1],[2,1,2],…etc]. How can I achieve this ?