Given an array of intergers, write a program to print all the permutations of the numbers in the array. The output should be sorted in a non-increasing order.
For example for the array {12, 4, 66, 8, 9}
, the output should be:
9866412
9866124
9846612
....
....
1246689
I thought of generating all permutations at the same time inserting them in a BST
, then performing reverse inorder on BST
.
This seems highly inefficient, as I'm storing the permutations, can we do better ?