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

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:3

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 ^{N}C_{X}

**Including a particluar man:**We can choose**(X – 1) men**from**(N – 1)**in.^{N – 1}C_{X – 1}**Excluding a particular man:**We can choose**X men**from**(N – 1)**in^{N – 1}C_{X}

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

10

## Recommended Posts:

- Ways of selecting men and women from a group to make a team
- Ways to form a group from three groups with given constraints
- Ways of dividing a group into two halves such that two elements are in different groups
- Total ways of choosing X men and Y women from a total of M men and W women
- Total number of ways to place X and Y at n places such that no two X are together
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Find sum of N-th group of Natural Numbers
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Number Theory | Generators of finite cyclic group under addition
- Total no of 1's in numbers
- Count total divisors of A or B in a given range
- Count total number of even sum sequences
- Count total number of digits from 1 to n

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.