Given two integers n and r, find the value of nPr, the number of ways to arrange r elements selected from n distinct elements. The value of nPr is given by: nPr = n! / (n − r)!, where ! denotes the factorial of a number.
Examples:
Input: n = 5, r = 2
Output: 20
Explanation: 5P2 = 5! / (5 - 2)! = 20
Input: n = 6, r = 3
Output: 120
Explanation: 6P3 = 6! / (6 - 3)! = 120
Table of Content
[Naive Approach] Using Factorial Formula - O(n) Time and O(1) Space
The idea is to compute n! and (n-r)! separately using an iterative factorial function and then apply the permutation formula: nPr = n! / (n-r)!
- If r > n, return 0.
- Compute n! using an iterative factorial function.
- Compute (n-r)! using the same function.
- Return n! / (n-r)!.
#include <iostream>
using namespace std;
// Function to calculate factorial
long long fact(int n)
{
long long result = 1;
for (int i = 2; i <= n; i++)
{
result *= i;
}
return result;
}
// Function to calculate nPr
long long nPr(int n, int r)
{
if (r > n)
return 0;
return fact(n) / fact(n - r);
}
int main()
{
int n = 5;
int r = 2;
cout << nPr(n, r) << endl;
return 0;
}
#include <stdio.h>
// Function to calculate factorial
long long fact(int n)
{
long long result = 1;
for (int i = 2; i <= n; i++)
{
result *= i;
}
return result;
}
// Function to calculate nPr
long long nPr(int n, int r)
{
if (r > n)
return 0;
return fact(n) / fact(n - r);
}
int main()
{
int n = 5;
int r = 2;
printf("%lld\n", nPr(n, r));
return 0;
}
class Solution {
// Function to calculate factorial
static long fact(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// Function to calculate nPr
static long nPr(int n, int r) {
if (r > n)
return 0;
return fact(n) / fact(n - r);
}
public static void main(String[] args) {
int n = 5;
int r = 2;
System.out.println(nPr(n, r));
}
}
# Function to calculate factorial
def fact(n: int) -> int:
result = 1
for i in range(2, n + 1):
result *= i
return result
# Function to calculate nPr
def nPr(n: int, r: int) -> int:
return fact(n) // fact(n - r)
if __name__ == "__main__":
n = 5
r = 2
print(nPr(n, r))
using System;
class GFG
{
// Function to calculate factorial
static long fact(int n)
{
long result = 1;
for (int i = 2; i <= n; i++)
{
result *= i;
}
return result;
}
// Function to calculate nPr
static long nPr(int n, int r)
{
if (r > n)
return 0;
return fact(n) / fact(n - r);
}
static void Main()
{
int n = 5;
int r = 2;
Console.WriteLine(nPr(n, r));
}
}
// Function to calculate factorial
function fact(n) {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// Function to calculate nPr
function nPr(n, r) {
if (r > n)
return 0;
return fact(n) / fact(n - r);
}
// Driver code
let n = 5;
let r = 2;
console.log(nPr(n, r));
Output
20
[Expected Approach] Multiply the Required Terms Directly - O(r) Time and O(1) Space
Instead of computing n! and (n-r)! separately, use the relation:
nPr = n × (n - 1) × (n - 2) × ... × (n - r + 1).This computes only the required r terms, avoiding unnecessary factorial calculations and making the solution more efficient.
#include <iostream>
using namespace std;
long long nPr(int n, int r)
{
if (r > n)
return 0;
long long ans = 1;
// Compute n × (n-1) × ... × (n-r+1)
for (int i = 0; i < r; i++)
ans *= (n - i);
return ans;
}
int main()
{
int n = 5;
int r = 2;
cout << nPr(n, r) << endl;
return 0;
}
#include <stdio.h>
long long nPr(int n, int r)
{
if (r > n)
return 0;
long long ans = 1;
// Compute n × (n-1) × ... × (n-r+1)
for (int i = 0; i < r; i++)
ans *= (n - i);
return ans;
}
int main()
{
int n = 5;
int r = 2;
printf("%lld\n", nPr(n, r));
return 0;
}
class GFG {
static long nPr(int n, int r) {
if (r > n)
return 0;
long ans = 1;
// Compute n × (n-1) × ... × (n-r+1)
for (int i = 0; i < r; i++)
ans *= (n - i);
return ans;
}
public static void main(String[] args) {
int n = 5;
int r = 2;
System.out.println(nPr(n, r));
}
}
def nPr(n: int, r: int) -> int:
if r > n:
return 0
ans = 1
# Compute n × (n-1) × ... × (n-r+1)
for i in range(r):
ans *= (n - i)
return ans
if __name__ == "__main__":
n = 5
r = 2
print(nPr(n, r))
using System;
class GFG
{
static long nPr(int n, int r)
{
if (r > n)
return 0;
long ans = 1;
// Compute n × (n-1) × ... × (n-r+1)
for (int i = 0; i < r; i++)
ans *= (n - i);
return ans;
}
static void Main()
{
int n = 5;
int r = 2;
Console.WriteLine(nPr(n, r));
}
}
function nPr(n, r) {
if (r > n)
return 0;
let ans = 1;
// Compute n × (n-1) × ... × (n-r+1)
for (let i = 0; i < r; i++)
ans *= (n - i);
return ans;
}
// Driver code
let n = 5;
let r = 2;
console.log(nPr(n, r));
Output
20