Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Generate Well Ordered Passwords of a Given Length K

Objec­tive: Gen­er­ate Well Ordered Pass­words of a Given Length K. Well ordered means that dig­its should be in increas­ing order in every gen­er­ated password.

Exam­ple:

K = 7

1234567 1234568 1234569 1234578 1234579 1234589 1234678 1234679 1234689 1234789 1235678 1235679 1235689 1235789 1236789 1245678 1245679 1245689 1245789 1246789 1256789 1345678 1345679 1345689 1345789 1346789 1356789 1456789 2345678 2345679 2345689 2345789 2346789 2356789 2456789 3456789

Approach:

This prob­lem is quite sim­i­lar to Print All N Length Strings from Given Num­ber K.

  • Loop through i = x to 9. ( all dig­its will be from 1 to 9).
  • x will start with 0 and will be incre­mented with every recur­sive call to make sure that pass­word will be well formed.
  • With every recur­sive call, mul­ti­ply the result (which will start with 0) by 10 and add i and make k = k-1.
  • Base case: when k becomes 0 (means array is full).
  • See the code for bet­ter explanation.

Code:

Output:
1234567 1234568 1234569 1234578 1234579 1234589
1234678 1234679 1234689 1234789 1235678 1235679
1235689 1235789 1236789 1245678 1245679 1245689
1245789 1246789 1256789 1345678 1345679 1345689
1345789 1346789 1356789 1456789 2345678 2345679
2345689 2345789 2346789 2356789 2456789 3456789

You may also like...