Number of ways to arrange K different objects taking N objects at a time
Last Updated :
31 May, 2022
Given two integers K and N, the task is to count the number of ways to arrange K different objects taking N objects at a time. Each arrangement contains one object at most once. The answer can be very large so return the answer modulo 109 + 7.
Note: 1 <= N <= K <= 105.
Prerequisites: Factorial of a number, Compute nCr % p
Examples:
Input : N = 3, K = 3
Output : 6
If 1, 2 and 3 be the K different objects, then the possible arrangements are {123, 132, 213, 231, 312, 321}.
Input : N = 4, K = 6
Output : 360
Approach: The problem can be solved using Permutation and Combination. As N is always less than or equal to K, an answer greater than 0 shall always exist. Any object among the K available objects can be used at most once in an arrangement. So, we need to select N objects out of the K available objects for a single arrangement. So, the total number of ways to select N objects from K objects is KCN . Any selected group of N objects can then be permuted among themselves in N ways. So, the answer to the problem is:
(N! * KCN) % (109 +7)
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod (ll)(1e9 + 7)
// Function to return n! % p
ll factorial(ll n, ll p)
{
ll res = 1; // Initialize result
for (int i = 2; i <= n; i++)
res = (res * i) % p;
return res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
ll nCrModP(ll n, ll r, ll p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
ll fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p) % p
* modInverse(fac[n - r], p) % p) % p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
ll countArrangements(ll n, ll k, ll p)
{
return (factorial(n, p) * nCrModP(k, n, p)) % p;
}
// Drivers Code
int main()
{
ll N = 5, K = 8;
// Function call
cout << countArrangements(N, K, mod);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static long mod =(long) (1e9 + 7);
// Function to return n! % p
static long factorial(long n, long p)
{
long res = 1; // Initialize result
for (int i = 2; i <= n; i++)
res = (res * i) % p;
return res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
static long nCrModP(long n, long r, long p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long fac[] = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)n] * modInverse(fac[(int)r], p) % p
* modInverse(fac[(int)n - (int)r], p) % p) % p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
static long countArrangements(long n, long k, long p)
{
return (factorial(n, p) * nCrModP(k, n, p)) % p;
}
// Driver Code
public static void main(String[] args)
{
long N = 5, K = 8;
// Function call
System.out.println(countArrangements(N, K, mod));
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
mod = 10**9 + 7
# Function to return n! % p
def factorial(n, p):
res = 1 # Initialize result
for i in range(2, n + 1):
res = (res * i) % p
return res
# Iterative Function to calculate
# (x^y)%p in O(log y)
def power(x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is
# more than or equal to p
while (y > 0):
# If y is odd,
# multiply x with result
if (y & 1):
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
# Returns n^(-1) mod p
def modInverse(n, p):
return power(n, p - 2, p)
# Returns nCr % p using Fermat's little
# theorem.
def nCrModP(n, r, p):
# Base case
if (r == 0):
return 1
# Fifactorial array so that we
# can find afactorial of r, n
# and n-r
fac = [0 for i in range(n + 1)]
fac[0] = 1
for i in range(1, n + 1):
fac[i] = fac[i - 1] * i % p
return (fac[n] * modInverse(fac[r], p) % p *
modInverse(fac[n - r], p) % p) % p
# Function to return the number of ways
# to arrange K different objects taking
# N objects at a time
def countArrangements(n, k, p):
return (factorial(n, p) *
nCrModP(k, n, p)) % p
# Drivers Code
N = 5
K = 8
# Function call
print(countArrangements(N, K, mod))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
static long mod =(long) (1e9 + 7);
// Function to return n! % p
static long factorial(long n, long p)
{
long res = 1; // Initialize result
for (int i = 2; i <= n; i++)
res = (res * i) % p;
return res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
static long nCrModP(long n, long r, long p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long[] fac = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)n] * modInverse(fac[(int)r], p) % p
* modInverse(fac[(int)n - (int)r], p) % p) % p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
static long countArrangements(long n, long k, long p)
{
return (factorial(n, p) * nCrModP(k, n, p)) % p;
}
// Driver Code
public static void Main()
{
long N = 5, K = 8;
// Function call
Console.WriteLine(countArrangements(N, K, mod));
}
}
/* This code is contributed by Code_Mech */
PHP
<?php
// PHP implementation of the approach
$mod = (1e9 + 7);
// Function to return n! % p
function factorial($n,$p)
{
$res = 1; // Initialize result
for ($i = 2; $i <= $n; $i++)
$res = ($res * $i) % $p;
return $res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
function power($x, $y, $p)
{
$res = 1; // Initialize result
$x = $x % $p; // Update x if it is more than or
// equal to p
while ($y > 0)
{
// If y is odd, multiply x with result
if (($y & 1)==1)
$res = ($res * $x) % $p;
// y must be even now
$y = $y >> 1; // y = y/2
$x = ($x * $x) % $p;
}
return $res;
}
// Returns n^(-1) mod p
function modInverse($n, $p)
{
return power($n, $p - 2, $p);
}
// Returns nCr % p using Fermat's little
// theorem.
function nCrModP($n, $r, $p)
{
// Base case
if ($r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
$fac= array((int)$n + 1);
$fac[0] = 1;
for ($i = 1; $i <= $n; $i++)
$fac[$i] = $fac[$i - 1] * $i % $p;
return ($fac[(int)$n] * modInverse($fac[(int)$r], $p) % $p
* modInverse($fac[(int)$n - (int)$r], $p) % $p) % $p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
function countArrangements($n, $k, $p)
{
return (factorial($n, $p) * nCrModP($k, $n, $p)) % $p;
}
// Driver Code
{
$N = 5; $K = 8;
// Function call
echo(countArrangements($N, $K, $mod));
}
/* This code is contributed by Code_Mech*/
JavaScript
<script>
// javascript implementation of the approach
var mod =parseInt(1e4 + 7);
// Function to return n! % p
function factorial(n , p)
{
var res = 1; // Initialize result
for (var i = 2; i <= n; i++)
res = (res * i) % p;
return res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
function power(x , y , p)
{
var res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
function modInverse(n , p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
function nCrModP(n , r , p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
var fac = Array.from({length: n+1}, (_, i) => 0);;
fac[0] = 1;
for (var i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[parseInt(n)] * modInverse(fac[parseInt(r)], p) % p
* modInverse(fac[parseInt(n) - parseInt(r)], p) % p) % p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
function countArrangements(n , k , p)
{
return (factorial(n, p) * nCrModP(k, n, p)) % p;
}
// Driver Code
var N = 5, K = 8;
// Function call
document.write(countArrangements(N, K, mod));
// This code contributed by Princi Singh
</script>
Time Complexity: O(n), to fill a factorial array
Auxiliary Space: O(n), extra space of size n used to make a factorial array
Similar Reads
Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles S(r, n), represents the number of ways that we can arrange r objects around indistinguishable circles of length n, and every circle n must have at least one object around it. Examples: Input: r = 9, n = 2Output: 109584Input: r = 6, n = 3Output: 225The special cases are: S(r, 0) = 0, trivial.S(r, 1)
15+ min read
Different Ways to Generate Permutations of an Array Permutations are like the magic wand of combinatorics, allowing us to explore the countless ways elements can be rearranged within an array. Whether you're a coder, a math enthusiast, or someone on a quest to solve a complex problem, understanding how to generate all permutations of an array is a va
6 min read
Number of ways to arrange N numbers which are in a range from 1 to K under given constraints. Given Four integers N, K, P and Q. The task is to calculate the number of ways to arrange N numbers which are in a range from 1 to K such that the first number is P, the last number is Q and no two adjacent numbers are consecutive. Examples: Input: N = 4, K = 3, P = 2, Q = 3 Output: 3 Explanation: F
11 min read
Number of ways to arrange N items under given constraints We are given N items which are of total K different colors. Items of the same color are indistinguishable and colors can be numbered from 1 to K and count of items of each color is also given as k1, k2, and so on. Now we need to arrange these items one by one under the constraint that the last item
10 min read
Number of ways to cut a stick of length N into K pieces Given a stick of size N, find the number of ways in which it can be cut into K pieces such that length of every piece is greater than 0. Examples : Input : N = 5 K = 2 Output : 4 Input : N = 15 K = 5 Output : 1001 Solving this question is equivalent to solving the mathematics equation x1 + x2 + ....
8 min read
Count number of ways to partition a set into k subsets Given two numbers n and k where n represents a number of elements in a set, find a number of ways to partition the set into k subsets.Example: Input: n = 3, k = 2Output: 3Explanation: Let the set be [1, 2, 3], we can partition it into 3 subsets in the following ways [[1,2], [3]], [[1], [2,3]], [[1,3
15+ min read
Number of different positions where a person can stand A person stands in the line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than 'f' people standing in front of him and no more than 'b' people standing behind him. The task is to find the number of different positions he can occupy. Examples:
3 min read
Count ways to arrange N distinct objects if all clockwise arrangements are considered the same Given N distinct objects, the task is to find the number of distinct arrangements of N objects if all clockwise arrangements are considered the same. If A, B, and C are three distinct objects, then arrangements {A, B, C}, {C, A, B}, and {B, C, A} are considered the same as all the arrangements are c
4 min read
Number of ways of distributing N identical objects in R distinct groups Given two integers N and R, the task is to calculate the number of ways to distribute N identical objects into R distinct groups. Examples: Input: N = 4, R = 2 Output: 5 No of objects in 1st group = 0, in second group = 4 No of objects in 1st group = 1, in second group = 3 No of objects in 1st group
5 min read
Total number of ways to place X and Y at n places such that no two X are together Given N positions, the task is to count the total number of ways to place X and Y such that no two X are together. Examples: Input: 3Output: 5XYX, YYX, YXY, XYY and YYY Input: 4Output: 8XYXY, XYYX, YXYX, YYYX, YYXY, YXYY, XYYY and YYYY Approach: For N = 1, X and Y are 2 possible ways. For N = 2, XY,
5 min read