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 NCX

  • Including a particluar 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

10


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.