Number of K 's such that the given array can be divided into two sets satisfying the given conditions | C ++ 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 ; Sort the given array ; Return number of such Ks ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int two_sets ( int a [ ] , int n ) { sort ( a , a + n ) ; return a [ n / 2 ] - a [ ( n / 2 ) - 1 ] ; } int main ( ) { int a [ ] = { 1 , 4 , 4 , 6 , 7 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << two_sets ( a , n ) ; return 0 ; }