+ 3

Possible combinations without repetition

Write a program to accept numbers in an array and from that numbers you have to print all possible combinations ex: if there are 5 numbers as 42163 then output should be like 12346, 12364,12436 and so on. So how to solve this?

9th Dec 2020, 5:46 PM
Achintya Raj
Achintya Raj - avatar
9 Answers
+ 9
show your attempt first
9th Dec 2020, 5:52 PM
ÃKR
ÃKR - avatar
+ 8
Achintya Raj try to find similar codes in code section , hope you get some idea 👍
9th Dec 2020, 6:01 PM
ÃKR
ÃKR - avatar
+ 6
For this kind of task you can use python module "itertools", then you can use permutations (as your description is showing) from itertools import permutations lst = [1,2,3,4,6] ... # your code follows here. ...
9th Dec 2020, 7:10 PM
Lothar
Lothar - avatar
+ 6
You want to print all distinct permutations of some number or string. You can see some algorithm for it here: https://www.geeksforgeeks.org/distinct-permutations-string-set-2/ I generally use next_permutation() of C++ stl, sort the string so that you can get lexicographically smallest string and then just keep finding next_permutation() until it returns false(means no next lexicographically greater permutation possible) //see method2 here https://www.geeksforgeeks.org/permutations-of-a-given-string-using-stl/
10th Dec 2020, 3:21 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Ok thanks
9th Dec 2020, 6:02 PM
Achintya Raj
Achintya Raj - avatar
0
I haven't tried yet as I am not getting the idea how the nested loops will be used
9th Dec 2020, 5:58 PM
Achintya Raj
Achintya Raj - avatar
0
Those nos which are made using these 5 digits with no repitition
9th Dec 2020, 7:00 PM
Achintya Raj
Achintya Raj - avatar
0
Sorry I want all the combinations I didn't read carefully that time Let me tell u with a small ex. Suppose there are three numbers 2 4 1 then possible combinations for this are : 241,214,412,421,124,142 i.e 6 combinations
9th Dec 2020, 7:33 PM
Achintya Raj
Achintya Raj - avatar