Related Articles

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

• Last Updated : 27 Apr, 2021

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

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 ``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);` `    ``cout << two_sets(a, n);` `    ``return` `0;``}`

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

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

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

## Javascript

 ``
Output:
`2`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up