# Number of K’s such that the given array can be divided into two sets satisfying the given conditions

Given an array **arr[]** of size **N**. The task is to find the number of **K**‘s such that the array can be divided into two sets containing equal number of elements if elements less than **K** are in one set and the rest of them are in the other set.

**Note**: **N** is always even.

**Examples:**

Input:arr[] = {9, 1, 4, 4, 6, 7}

Output:2

{1, 4, 4} and {6, 7, 9} are the two sets.

K can be 5 or 6.

Input:arr[] = {1, 2, 3, 3, 4, 5}

Output:0

**Approach:** An efficient approach is to sort the array. Then if two middle numbers are the same then the answer is zero otherwise answer is the difference between the two numbers.

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 count of K's ` `// such that the array can be divided ` `// into two sets containing equal number ` `// of elements when all the elements less ` `// than K are in one set and the rest ` `// of the elements are in the other set ` `int` `two_sets(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Sort the given array ` ` ` `sort(a, a + n); ` ` ` ` ` `// Return number of such Ks ` ` ` `return` `a[n / 2] - a[(n / 2) - 1]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 4, 4, 6, 7, 9 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `cout << two_sets(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of K's ` `// such that the array can be divided ` `// into two sets containing equal number ` `// of elements when all the elements less ` `// than K are in one set and the rest ` `// of the elements are in the other set ` `static` `int` `two_sets(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Sort the given array ` ` ` `Arrays.sort(a); ` ` ` ` ` `// Return number of such Ks ` ` ` `return` `a[n / ` `2` `] - a[(n / ` `2` `) - ` `1` `]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `4` `, ` `4` `, ` `6` `, ` `7` `, ` `9` `}; ` ` ` `int` `n = a.length; ` ` ` ` ` `System.out.println(two_sets(a, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of K's ` `# such that the array can be divided ` `# into two sets containing equal number ` `# of elements when all the elements less ` `# than K are in one set and the rest ` `# of the elements are in the other set ` `def` `two_sets(a, n) : ` ` ` ` ` `# Sort the given array ` ` ` `a.sort(); ` ` ` ` ` `# Return number of such Ks ` ` ` `return` `(a[n ` `/` `/` `2` `] ` `-` `a[(n ` `/` `/` `2` `) ` `-` `1` `]); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `1` `, ` `4` `, ` `4` `, ` `6` `, ` `7` `, ` `9` `]; ` ` ` `n ` `=` `len` `(a); ` ` ` ` ` `print` `(two_sets(a, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of K's ` `// such that the array can be divided ` `// into two sets containing equal number ` `// of elements when all the elements less ` `// than K are in one set and the rest ` `// of the elements are in the other set ` `static` `int` `two_sets(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `// Sort the given array ` ` ` `Array.Sort(a); ` ` ` ` ` `// Return number of such Ks ` ` ` `return` `a[n / 2] - a[(n / 2) - 1]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[]a = { 1, 4, 4, 6, 7, 9 }; ` ` ` `int` `n = a.Length; ` ` ` ` ` `Console.WriteLine(two_sets(a, n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count valid pairs in the array satisfying given conditions
- Generate N integers satisfying the given conditions
- First N natural can be divided into two sets with given difference and co-prime sums
- Check if elements of an array can be arranged satisfying the given condition
- Find minimum number to be divided to make a number a perfect square
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Generate elements of the array following given conditions
- Find remainder of array multiplication divided by n
- Check if a sorted array can be divided in pairs whose sum is k
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Check if an array can be divided into pairs whose sum is divisible by k
- Maximize array sum by X increments when each element is divided by 10
- Find if array can be divided into two subarrays of equal sum
- Split the array into equal sum parts according to given conditions
- Maximum length sub-array which satisfies the given conditions
- Maximum length sub-array which satisfies the given conditions
- Generate an array of size K which satisfies the given conditions
- Find an array of size N that satisfies the given conditions
- Count of triplets in an array that satisfy the given conditions
- Generate an array using given conditions from a given array

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.