Permutations of n things taken r at a time with k things together - GeeksforGeeks

08 Mar, 2021
Given n, r and K. The task is to find the number of permutations of different things taken at a time such that specific things always occur together.Examples:  
Input : n = 8, r = 5, k = 2
Output : 960

Input : n = 6, r = 2, k = 2
Output : 2
 
Approach:  
A bundle of specific things can be put in r places in (r – k + 1) ways .k specific things in the bundle can be arranged themselves into k! ways.Now (n – k) things will be arranged in (r – k) places in ways.
A bundle of specific things can be put in r places in (r – k + 1) ways .
k specific things in the bundle can be arranged themselves into k! ways.
Now (n – k) things will be arranged in (r – k) places in ways.
Thus, using the fundamental principle of counting, the required number of permutations will be:  
 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find the number of permutations of// n different things taken r at a time// with k things grouped together #include <bits/stdc++.h>using namespace std; // Function to find factorial// of a numberint factorial(int n){    int fact = 1;     for (int i = 2; i <= n; i++)        fact = fact * i;     return fact;} // Function to calculate p(n, r)int npr(int n, int r){    int pnr = factorial(n) / factorial(n - r);     return pnr;} // Function to find the number of permutations of// n different things taken r at a time// with k things grouped togetherint countPermutations(int n, int r, int k){    return factorial(k) * (r - k + 1) * npr(n - k, r - k);} // Driver codeint main(){    int n = 8;    int r = 5;    int k = 2;     cout << countPermutations(n, r, k);     return 0;}
// Java program to find the number of permutations of// n different things taken r at a time// with k things grouped together class GFG{// Function to find factorial// of a numberstatic int factorial(int n){    int fact = 1;     for (int i = 2; i <= n; i++)        fact = fact * i;     return fact;} // Function to calculate p(n, r)static int npr(int n, int r){    int pnr = factorial(n) / factorial(n - r);     return pnr;} // Function to find the number of permutations of// n different things taken r at a time// with k things grouped togetherstatic int countPermutations(int n, int r, int k){    return factorial(k) * (r - k + 1) * npr(n - k, r - k);} // Driver codepublic static void main(String[] args){    int n = 8;    int r = 5;    int k = 2;     System.out.println(countPermutations(n, r, k));}}// this code is contributed by mits
# Python3 program to find the number of permutations of# n different things taken r at a time# with k things grouped together # def to find factorial# of a numberdef factorial(n):      fact = 1;     for i in range(2,n+1):        fact = fact * i;     return fact;   # def to calculate p(n, r)def npr(n, r):      pnr = factorial(n) / factorial(n - r);     return pnr;   # def to find the number of permutations of# n different things taken r at a time# with k things grouped togetherdef countPermutations(n, r, k):      return int(factorial(k) * (r - k + 1) * npr(n - k, r - k));   # Driver coden = 8;r = 5;k = 2; print(countPermutations(n, r, k));     # this code is contributed by mits
// C# program to find the number of// permutations of n different things// taken r at a time with k things// grouped togetherusing System; class GFG{     // Function to find factorial// of a numberstatic int factorial(int n){    int fact = 1;     for (int i = 2; i <= n; i++)        fact = fact * i;     return fact;} // Function to calculate p(n, r)static int npr(int n, int r){    int pnr = factorial(n) /              factorial(n - r);     return pnr;} // Function to find the number of// permutations of n different// things taken r at a time with// k things grouped togetherstatic int countPermutations(int n,                             int r, int k){    return factorial(k) * (r - k + 1) *                    npr(n - k, r - k);} // Driver codestatic void Main(){    int n = 8;    int r = 5;    int k = 2;     Console.WriteLine(countPermutations(n, r, k));}} // This code is contributed by mits
<?php// PHP program to find the number// of permutations of n different// things taken r at a time// with k things grouped together // Function to find factorial// of a numberfunction factorial($n){    $fact = 1;     for ($i = 2; $i <= $n; $i++)        $fact = $fact * $i;     return $fact;} // Function to calculate p(n, r)function npr($n, $r){    $pnr = factorial($n) /           factorial($n - $r);     return $pnr;} // Function to find the number of// permutations of n different// things taken r at a time// with k things grouped togetherfunction countPermutations($n, $r, $k){    return factorial($k) * ($r - $k + 1) *                   npr($n - $k, $r - $k);} // Driver code$n = 8;$r = 5;$k = 2; echo countPermutations($n, $r, $k); // This code is contributed by mits?>
<script> // JavaScript program to find the number of permutations of// n different things taken r at a time// with k things grouped together // Function to find factorial// of a numberfunction factorial(n){    let fact = 1;     for (let i = 2; i <= n; i++)        fact = fact * i;     return fact;} // Function to calculate p(n, r)function npr(n, r){    let pnr = Math.floor(factorial(n) / factorial(n - r));     return pnr;} // Function to find the number of permutations of// n different things taken r at a time// with k things grouped together function countPermutations(n, r, k){    return factorial(k) * (r - k + 1) * npr(n - k, r - k);} // Driver code    let n = 8;    let r = 5;    let k = 2;     document.write(countPermutations(n, r, k)); // This code is contributed by Surbhi Tyagi. </script>
960
 
Mithun Kumar
surbhityagi15
Permutation and Combination
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Count all possible paths from top left to bottom right of a mXn matrix
Modular multiplicative inverse
Fizz Buzz Implementation
Program to multiply two matrices
Check if a number is Palindrome
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python