-
Notifications
You must be signed in to change notification settings - Fork 77
/
NthPermutation.cpp
44 lines (40 loc) · 998 Bytes
/
NthPermutation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Permutation of a given number is the number of ways
// one can arrange the digits of the number & obtain
// unique numbers.
// There can be 'n!' permutations for a number with 'n' digits.
// This program gives you the 'k'th permutation for the given number
// without figuring out the other permutations.
#include <bits/stdc++.h>
using namespace std;
int getNthPermutation(int n, int k)
{
k--;
int res = 0;
vector<int> nums;
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact = fact * i;
nums.push_back(i);
}
// fact = fact / n;
while (n > 0)
{
fact = fact / n;
res = res * 10 + nums[k / fact];
nums.erase(nums.begin() + k / fact);
k = k % fact;
n--;
}
return res;
}
int main()
{
int n;
int k;
cout << "Enter the no. of digits of your number : ";
cin >> n;
cout << "Enter the no of permutation you want : ";
cin >> k;
cout << getNthPermutation(n, k) << endl;
}