#include <cstdio>
#include <algorithm>

using namespace std;

int a[500005]={};

int abs(int a)
{
    if(a<0)
        a*=-1;
    return a;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    
    sort(a,a+n);
    int md=500005;
    int ma=500005;
    int asl=1000000005;

    for(int i=1; i<n; i++)
    {
        if(a[i-1] == a[i])
            continue;
        if(abs(i-(n-i)) < md)
        {
            md=abs(i-(n-i));
            ma=n-i;
            asl=a[i];
        }
        else if(abs(i-(n-i)) == md)
        {
            if(n-i < ma)
            {
                ma=n-i;
                asl=a[i];
            }
        }
    }
    printf("%d",asl);
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: