# Total ways of selecting a group of X men from N men with or without including a particular man

• Last Updated : 13 Mar, 2022

Given two integers X and N. The task is to find the total number of ways of selecting X men from a group of N men with or without including a particular man.
Examples:

Input: N = 3 X = 2
Output:
Including a man say M1, the ways can be (M1, M2) and (M1, M3).
Excluding a man say M1, the only way is (M2, M3).
Total ways = 2 + 1 = 3.
Input: N = 5 X = 3
Output: 10

Approach: The total number of ways of choosing X men from N men is NCX

• Including a particular man: We can choose (X – 1) men from (N – 1) in N – 1CX – 1.
• Excluding a particular man: We can choose X men from (N – 1) in N – 1CX

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the value of nCr``int` `nCr(``int` `n, ``int` `r)``{` `    ``// Initialize the answer``    ``int` `ans = 1;` `    ``for` `(``int` `i = 1; i <= r; i += 1) {` `        ``// Divide simultaneously by``        ``// i to avoid overflow``        ``ans *= (n - r + i);``        ``ans /= i;``    ``}``    ``return` `ans;``}` `// Function to return the count of ways``int` `total_ways(``int` `N, ``int` `X)``{``    ``return` `(nCr(N - 1, X - 1) + nCr(N - 1, X));``}` `// Driver code``int` `main()``{``    ``int` `N = 5, X = 3;` `    ``cout << total_ways(N, X);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.io.*;` `class` `GFG``{``    ` `// Function to return the value of nCr``static` `int` `nCr(``int` `n, ``int` `r)``{` `    ``// Initialize the answer``    ``int` `ans = ``1``;` `    ``for` `(``int` `i = ``1``; i <= r; i += ``1``)``    ``{` `        ``// Divide simultaneously by``        ``// i to avoid overflow``        ``ans *= (n - r + i);``        ``ans /= i;``    ``}``    ``return` `ans;``}` `// Function to return the count of ways``static` `int` `total_ways(``int` `N, ``int` `X)``{``    ``return` `(nCr(N - ``1``, X - ``1``) + nCr(N - ``1``, X));``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``int` `N = ``5``, X = ``3``;``    ` `    ``System.out.println (total_ways(N, X));``}``}` `// This code is contributed by Sachin`

## Python3

 `# Python3 implementation of the approach` `# Function to return the value of nCr``def` `nCr(n, r) :` `    ``# Initialize the answer``    ``ans ``=` `1``;` `    ``for` `i ``in` `range``(``1``, r ``+` `1``) :` `        ``# Divide simultaneously by``        ``# i to avoid overflow``        ``ans ``*``=` `(n ``-` `r ``+` `i);``        ``ans ``/``/``=` `i;` `    ``return` `ans;` `# Function to return the count of ways``def` `total_ways(N, X) :` `    ``return` `(nCr(N ``-` `1``, X ``-` `1``) ``+` `nCr(N ``-` `1``, X));` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``N ``=` `5``; X ``=` `3``;` `    ``print``(total_ways(N, X));` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;``    ` `class` `GFG``{``    ` `// Function to return the value of nCr``static` `int` `nCr(``int` `n, ``int` `r)``{` `    ``// Initialize the answer``    ``int` `ans = 1;` `    ``for` `(``int` `i = 1; i <= r; i += 1)``    ``{` `        ``// Divide simultaneously by``        ``// i to avoid overflow``        ``ans *= (n - r + i);``        ``ans /= i;``    ``}``    ``return` `ans;``}` `// Function to return the count of ways``static` `int` `total_ways(``int` `N, ``int` `X)``{``    ``return` `(nCr(N - 1, X - 1) + nCr(N - 1, X));``}` `// Driver code``public` `static` `void` `Main (String[] args)``{``    ``int` `N = 5, X = 3;``    ` `    ``Console.WriteLine(total_ways(N, X));``}``}``    ` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`10`

Time Complexity: O(X)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up