Probability such that two subset contains same number of elements

Given a set containing N elements. If two subset X and Y picked then find the probability that both of them contains the same number of elements.

Examples:

Input: 4
Output: 35/128
Input: 2
Output: 3/8

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
Let’s choose a subset X that has r number of elements then Y must contain r number of elements. A subset can have minimum 0 elements and maximum N elements.

Total number of subset of a set contains N number of elements is , Total possible way to choose X and Y simultaneously will be = = .

Let, P = Total possible way to choose X and Y such that both has same number of elements.

Then P = = =

So the required probability will be .

Below is the implementation of the above Approach:

C++

 // C++ implementation of  // the above approach  #include  using namespace std;     // Returns value of Binomial  // Coefficient C(n, k)  int binomialCoeff(int n, int k)  {      int res = 1;         // Since C(n, k) = C(n, n-k)      if (k > n - k)          k = n - k;         // Calculate value of      for (int i = 0; i < k; ++i) {          res *= (n - i);          res /= (i + 1);      }         return res;  }     // Iterative Function to  // calculate (x^y) in O(log y)  int power(int x, unsigned int y)  {      // Initialize result      int res = 1;         while (y > 0) {             // If y is odd, multiply          // x with result          if (y & 1)              res = res * x;             // y must be even now          // y = y/2          y = y >> 1;             // Change x to x^2          x = x * x;      }      return res;  }     // Function to find probability  void FindProbability(int n)  {         // Calculate total possible      // ways and favourable ways.      int up = binomialCoeff(2 * n, n);      int down = power(2, 2 * n);         // Divide by gcd such that      // they become relatively coprime      int g = __gcd(up, down);         up /= g, down /= g;         cout << up << "/" << down << endl;  }     // Driver code  int main()  {         int N = 8;         FindProbability(N);         return 0;  }

Java

 // Java implementation of   // the above approach   class GFG  {             // Returns value of Binomial       // Coefficient C(n, k)       static int binomialCoeff(int n, int k)       {           int res = 1;                  // Since C(n, k) = C(n, n-k)           if (k > n - k)               k = n - k;                  // Calculate value of           for (int i = 0; i < k; ++i)           {               res *= (n - i);               res /= (i + 1);           }                  return res;       }              // Iterative Function to       // calculate (x^y) in O(log y)       static int power(int x, int y)       {           // Initialize result           int res = 1;                  while (y > 0)           {                      // If y is odd, multiply               // x with result               if ((y & 1) == 1)                   res = res * x;                      // y must be even now               // y = y/2               y = y >> 1;                      // Change x to x^2               x = x * x;           }           return res;       }              // Recursive function to return gcd of a and b       static int gcd(int a, int b)       {           if (b == 0)               return a;           return gcd(b, a % b);                  }              // Function to find probability       static void FindProbability(int n)       {                  // Calculate total possible           // ways and favourable ways.           int up = binomialCoeff(2 * n, n);           int down = power(2, 2 * n);                  // Divide by gcd such that           // they become relatively coprime           int g = gcd(up, down);                  up /= g;          down /= g;                  System.out.println(up + "/" + down);       }              // Driver code       public static void main (String[] args)      {           int N = 8;                  FindProbability(N);       }   }     // This code is contributed by AnkitRai01

Python3

 # Python3 implementation of   # the above approach   import math     # Returns value of Binomial   # Coefficient C(n, k)   def binomialCoeff(n, k):         res = 1        # Since C(n, k) = C(n, n-k)       if (k > n - k):           k = n - k          # Calculate value of       for i in range(0, k):           res = res * (n - i)           res = res // (i + 1)         return res     # Iterative Function to   # calculate (x^y) in O(log y)   def power(x, y):             # Initialize result       res = 1        while (y > 0):             # If y is odd, multiply           # x with result           if (y & 1):              res = res * x             # y must be even now           # y = y/2           y = y // 2            # Change x to x^2           x = x * x             return res      # Function to find probability   def FindProbability(n):         # Calculate total possible       # ways and favourable ways.       up = binomialCoeff(2 * n, n)      down = power(2, 2 * n)         # Divide by gcd such that       # they become relatively coprime       g = math.gcd(up,down)         up = up // g      down = down // g         print(up, "/", down)     # Driver code   N = 8 FindProbability(N)     # This code is contributed by Sanjit_Prasad

C#

 // C# implementation of   // the above approach   using System;  using System.Collections.Generic;         class GFG  {             // Returns value of Binomial       // Coefficient C(n, k)       static int binomialCoeff(int n, int k)       {           int res = 1;                  // Since C(n, k) = C(n, n-k)           if (k > n - k)               k = n - k;                  // Calculate value of           for (int i = 0; i < k; ++i)           {               res *= (n - i);               res /= (i + 1);           }                  return res;       }              // Iterative Function to       // calculate (x^y) in O(log y)       static int power(int x, int y)       {           // Initialize result           int res = 1;                  while (y > 0)           {                      // If y is odd, multiply               // x with result               if ((y & 1) == 1)                   res = res * x;                      // y must be even now               // y = y/2               y = y >> 1;                      // Change x to x^2               x = x * x;           }           return res;       }              // Recursive function to      // return gcd of a and b       static int gcd(int a, int b)       {           if (b == 0)               return a;           return gcd(b, a % b);       }              // Function to find probability       static void FindProbability(int n)       {                  // Calculate total possible           // ways and favourable ways.           int up = binomialCoeff(2 * n, n);           int down = power(2, 2 * n);                  // Divide by gcd such that           // they become relatively coprime           int g = gcd(up, down);                  up /= g;          down /= g;                  Console.WriteLine(up + "/" + down);       }              // Driver code       public static void Main (String[] args)      {           int N = 8;                  FindProbability(N);       }   }     // This code is contributed by 29AjayKumar

Output:

6435/32768


GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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.