-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationCoefficient.cpp
More file actions
65 lines (65 loc) · 1.4 KB
/
PermutationCoefficient.cpp
File metadata and controls
65 lines (65 loc) · 1.4 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
using namespace std;
//Time complexity is O(n*r) and Space Complexity is O(n*r)
int perCof(int n,int r)
{
int **ar=new int*[n+1];
for(int i=0;i<=n;i++)
{
ar[i]=new int[r+1];
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=r;j++)
{
ar[i][j]=0;
}
}
ar[0][0]=1;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=min(i,r);j++)
{
if(j==0)
{
ar[i][j]=1;
}
else
{
ar[i][j]=ar[i-1][j]+j*ar[i-1][j-1];
}
}
}
return ar[n][r];
}
//Time Complexity is O(n) And Space Complexity is O(n)
int perCof2(int n,int r)
{
int *ar=new int[n+1];
ar[0]=1;
for(int i=1;i<=n;i++)
{
ar[i]=ar[i-1]*i;
}
return ar[n]/ar[n-r];
}
//Time Complexity is O(n) And Space Complexity is O(1)
int perCof3(int n,int r)
{
int res=1;
for(int i=n-r+1;i<=n;i++)
{
res*=i;
}
return res;
}
int main()
{
int n,r;
cout<<"Enter the value of n and r:\n";
cin>>n;
cin>>r;
cout<<"Permutation Coefficient of P("<<n<<","<<r<<") is :"<<perCof(n,r)<<endl;
cout<<"Permutation Coefficient of P("<<n<<","<<r<<") is :"<<perCof2(n,r)<<endl;
cout<<"Permutation Coefficient of P("<<n<<","<<r<<") is :"<<perCof3(n,r)<<endl;
}