Language: C++

#include <functional>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <cctype>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <queue>
#include <stdio.h>
#include <stack>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <assert.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define TR(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++)
#define ALL(x) x.begin(),x.end()
#define SORT(x) sort(ALL(x))
#define CLEAR(x) memset(x,0,sizeof(x))
#define FILL(x,c) memset(x,c,sizeof(x))
#define PB push_back
#define MP make_pair

using namespace std;

const double eps = 1e-8;

typedef map<int,int> MII;
typedef map<string,int> MSI;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long double> VD;
typedef pair<int,int> PII;
typedef long long int64;
typedef long long LL;
typedef unsigned int UI;
typedef long double LD;

int isKValid(vector<int>& d, int k) {
    //cout<<"isKValid k:"<<k<<endl;
    int ecnt = 0, dcnt = 0;
    for (auto& n : d) {
        if (n >= k) dcnt++;
        else ecnt++;
    }
    int ans = 0;
    if (ecnt == dcnt) ans = 0;
    else if (ecnt < dcnt) ans = -1;
    else ans = 1;
    //cout<<"ans:"<<ans<<endl;
    return ans;
}

int main() {
    int n;
    cin >> n;
    vector<int> d(n, 0);
    int l = 1, h = 100000, m = 0, lm = INT_MAX, hm = INT_MIN;
    for (int i = 0; i < n; ++i) {
        cin >> d[i];
        if (lm > d[i]) lm = d[i];
        if (hm < d[i]) hm = d[i];
    }
    int lb = 0, ub = 0;

    // found the upper bound
    l = lm, h = hm;
    while (l < h) {
        m = l / 2 + h / 2;
        //cout<<"l:"<<l<<" h:"<<h<<" m:"<<m<<endl;
        int r = isKValid(d, m);
        if (r < 0) {
            // k is small
            l = m + 1;
        } else if (r > 0) {
            // k is big
            h = m;
        } else {
            l = m;
            if (l == h - 1) {
                if (isKValid(d, h) == 0) {
                    l = h;
                    break;
                } else {
                    break;
                }
            }
        }
    }
    ub = l;
    //cout<<"upper bound:"<<ub<<endl;

    // found the lower bound
    l = lm, h = hm;
    while (l < h) {
        m = l / 2 + h / 2;
        //cout<<"l:"<<l<<" h:"<<h<<" m:"<<m<<endl;
        int r = isKValid(d, m);
        if (r < 0) {
            // k is small
            l = m + 1;
        } else if (r > 0) {
            // k is big
            h = m;
        } else {
            h = m + 1;
            if (m == l + 1) {
                if (isKValid(d, l) == 0) {
                    break;
                } else {
                    l = m;
                    break;
                }
            }
        }
    }
    lb = l;

    //cout<<"lower bound:"<<lb<<endl;

    if (isKValid(d, ub) == 0 && isKValid(d, ub) == 0) {
        cout<<ub - lb + 1<<endl;
    } else {
        cout<<0<<endl;
    }

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:70:40: error: 'INT_MAX' was not declared in this scope
   70 |     int l = 1, h = 100000, m = 0, lm = INT_MAX, hm = INT_MIN;
      |                                        ^~~~~~~
a.cc:27:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   26 | #include <assert.h>
  +++ |+#include <climits>
   27 | #define REP(i,n) for(int i=0;i<n;i++)
a.cc:74:13: error: 'hm' was not declared in this scope; did you mean 'lm'?
   74 |         if (hm < d[i]) hm = d[i];
      |             ^~
      |             lm
a.cc:79:17: error: 'hm' was not declared in this scope; did you mean 'lm'?
   79 |     l = lm, h = hm;
      |                 ^~
      |                 lm