We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
-You may be tempted to create a vector<> of all possible permutations using next_permutation to easily access the ith element; there are far to many permutations needed to use this method given the time limits
-There are N! permutations of a char within a string of size N, and there are further {(N-1)! to (N=1)!}permutations:
Recursive function
Start call with kth permutation and original string
Find factorial of string.size()-1
Find X = k/factorial and Y = k%factorial
Hold the Xth character, j, of string and remove it from string
Return j and call function (Y,string)
Print function's return
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #24: Lexicographic permutations
You are viewing a single comment's thread. Return to all comments →
C++ USERS
Tips:
-You may be tempted to create a
vector<>
of all possible permutations usingnext_permutation
to easily access the ith element; there are far to many permutations needed to use this method given the time limits-There are N! permutations of a char within a string of size N, and there are further {(N-1)! to (N=1)!}permutations:
function
k
th permutation and originalstring
factorial
of string.size()-1X
= k/factorial andY
= k%factorialX
th character,j
, ofstring
and remove it fromstring
j
and callfunction
(Y,string
)function
's return