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

Given n, r and K. The task is to find the number of permutations of n different things taken r at a time such that k 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:



  1. A bundle of k specific things can be put in r places in (r – k + 1) ways .
  2. k specific things in the bundle can be arranged themselves into k! ways.
  3. Now (n – k) things will be arranged in (r – k) places in $n-k_{P_r-k}$ ways.

Thus, using the fundamental principle of counting, the required number of permutations will be:

        Permutations = k! \times (r-k+1) \times $n-k_{P_r-k}$

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 number
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)
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 together
int countPermutations(int n, int r, int k)
{
    return factorial(k) * (r - k + 1) * npr(n - k, r - k);
}
  
// Driver code
int main()
{
    int n = 8;
    int r = 5;
    int k = 2;
  
    cout << countPermutations(n, r, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 number
static 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 together
static int countPermutations(int n, int r, int k)
{
    return factorial(k) * (r - k + 1) * npr(n - k, r - k);
}
  
// Driver code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 number 
def 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 together 
def countPermutations(n, r, k): 
   
    return int(factorial(k) * (r - k + 1) * npr(n - k, r - k)); 
   
  
# Driver code 
n = 8
r = 5
k = 2
  
print(countPermutations(n, r, k));
      
# this code is contributed by mits 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the number of
// permutations of n different things
// taken r at a time with k things 
// grouped together 
using System;
  
class GFG
{
      
// Function to find factorial 
// of a number 
static 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 together 
static int countPermutations(int n, 
                             int r, int k) 
    return factorial(k) * (r - k + 1) * 
                    npr(n - k, r - k); 
  
// Driver code 
static void Main() 
    int n = 8; 
    int r = 5; 
    int k = 2; 
  
    Console.WriteLine(countPermutations(n, r, k)); 
  
// This code is contributed by mits 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 number
function 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 together
function 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
?>

chevron_right


Output:

960


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar