# Ways of dividing a group into two halves such that two elements are in different groups

Given 2n girls and randomly divided into two subgroups each containing n girls. The task is to count the number of ways in which groups can be formed such that two beautiful girls are into different groups.

**Example: **

Input:4

Output:4

Let group be r1, r2, b1, b2 where b1 and b2 are beautiful girls

Groups are: ((r1, b1) (r2, b2)), ((r1, b2) (r2, b1)), ((r2, b2) (r1, b1)), ((r2, b1) (r1, b2))

Input:8

Output:40

**Approach: **There are two ways in which the two beautiful girls lie in different groups and corresponding to each way the remaining (2n – 2) girls can be divided into two groups is

Hence total number of ways are 2 *

** Implementation Code : **

## C++

`// CPP Program to count ` `// Number of ways in which two ` `// Beautiful girls are in different group ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// This function will ` `// return the factorial of a given number ` `int` `factorial(` `int` `n) ` `{ ` ` ` `int` `result = 1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `result = result * i; ` ` ` `return` `result; ` `} ` ` ` `// This function will calculate nCr of given ` `// n and r ` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `return` `factorial(n) / (factorial(r) * factorial(n - r)); ` `} ` ` ` `// This function will ` `// Calculate number of ways ` `int` `calculate_result(` `int` `n) ` `{ ` ` ` `int` `result = 2 * nCr((n - 2), (n / 2 - 1)); ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `int` `main(` `void` `) ` `{ ` ` ` `int` `a = 2, b = 4; ` ` ` `cout << calculate_result(2 * a) << endl; ` ` ` `cout << calculate_result(2 * b) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

` ` `//Java Program to count ` `// Number of ways in which two ` `// Beautiful girls are in different group ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// This function will ` `// return the factorial of a given number ` `static` `int` `factorial(` `int` `n) ` `{ ` ` ` `int` `result = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `result = result * i; ` ` ` `return` `result; ` `} ` ` ` `// This function will calculate nCr of given ` `// n and r ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `return` `factorial(n) / (factorial(r) * factorial(n - r)); ` `} ` ` ` `// This function will ` `// Calculate number of ways ` `static` `int` `calculate_result(` `int` `n) ` `{ ` ` ` `int` `result = ` `2` `* nCr((n - ` `2` `), (n / ` `2` `- ` `1` `)); ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `a = ` `2` `, b = ` `4` `; ` ` ` `System.out.println( calculate_result(` `2` `* a)); ` ` ` `System.out.print(calculate_result(` `2` `* b)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to count ` `# Number of ways in which two ` `# Beautiful girls are in different group ` ` ` `# This function will ` `# return the factorial of a ` `# given number ` `def` `factorial(n) : ` ` ` ` ` `result ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `) : ` ` ` `result ` `*` `=` `i ` ` ` ` ` `return` `result ` ` ` `# This function will calculate nCr of given ` `# n and r ` `def` `nCr(n, r) : ` ` ` ` ` `return` `(factorial(n) ` `/` `/` `(factorial(r) ` ` ` `*` `factorial(n ` `-` `r))) ` ` ` ` ` `# This function will ` `# Calculate number of ways ` `def` `calculate_result(n) : ` ` ` ` ` `result ` `=` `2` `*` `nCr((n ` `-` `2` `), (n ` `/` `/` `2` `-` `1` `)) ` ` ` ` ` `return` `result ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a, b ` `=` `2` `, ` `4` ` ` `print` `(calculate_result(` `2` `*` `a)) ` ` ` `print` `(calculate_result(` `2` `*` `b)) ` ` ` `# This code is contributed by ` `# ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`//C# Program to count ` `// Number of ways in which two ` `// Beautiful girls are in different groupusing System; ` ` ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` `// This function will ` `// return the factorial of a given number ` `static` `int` `factorial(` `int` `n) ` `{ ` ` ` `int` `result = 1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `result = result * i; ` ` ` `return` `result; ` `} ` ` ` `// This function will calculate nCr of given ` `// n and r ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `return` `factorial(n) / (factorial(r) * factorial(n - r)); ` `} ` ` ` `// This function will ` `// Calculate number of ways ` `static` `int` `calculate_result(` `int` `n) ` `{ ` ` ` `int` `result = 2 * nCr((n - 2), (n / 2 - 1)); ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `a = 2, b = 4; ` ` ` `Console.WriteLine( calculate_result(2 * a)); ` ` ` `Console.Write(calculate_result(2 * b)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Subhadeep ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

4 40

## Recommended Posts:

- Dividing an array into two halves of same sum
- Ways of selecting men and women from a group to make a team
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Ways to sum to N using array elements with repetition allowed
- Ways to multiply n elements with an associative operation
- Number of ways to form an array with distinct adjacent elements
- Maximum value with the choice of either dividing or considering as it is
- Maximum sum after repeatedly dividing N by a divisor
- Count of divisors having more set bits than quotient on dividing N
- Find sum of N-th group of Natural Numbers
- Divide 1 to n into two groups with minimum sum difference
- Number of Groups of Sizes Two Or Three Divisible By 3
- Split N^2 numbers into N groups of equal sum
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Maximum number of 3-person teams formed from two groups

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.