Language: C++

#include <bits/stdc++.h>
using namespace std;
int main() {
    int N;
    long long int K;
    cin >> N >> K;
    long long int A[N];
    int i,j;
    for(i=0;i<N;i++)cin >> A[i];
    sort(A,A+N);
    int plus=0;
    int zero=0;
    int minus=0;
    long long int pair_pl,pair_zr,pair_mn;
    for(i=0;i<N;i++){
        if(A[i]<0)minus++;
        else if(A[i]==0)zero++;
        else if(A[i]>0)plus++;
    }
    pair_pl=minus*(minus-1)+plus*(plus-1);
    pair_zr=zero*(plus+minus+zero-1);
    pair_mn=plus*minus;
    if(K<=pair_mn){
        long long int min = A[0]*A[N-1];
        long long int max = 0;
        while(max!=min){
            long long int mid = (max+min)/2;
            long long int count = 0;
            int tmp = N-1;      
            for(i=0;i<minus;i++){
                int flag =0; 
                for(j=tmp;j>N-1-plus;j--){
                    if(A[i]*A[j]>mid){
                        flag++;
                        tmp=j+1;
                        count+=(N-1-j);
                        break;
                    }
                }
                if(flag==0)count+=plus;
                if(count>K)break;
            }
            if(count<=K)min=mid;
            if(count>=K)max=mid;
        }
        cout << mid << endl;
    }
    else if(K<=pair_mn+pair_zr)cout << 0 << endl; 
    else{
        long long int ans = K-(pair_mn+pair_zr);
        long long int min = 0;
        long long int max = max(A[N-1]*A[N-1],A[0]*A[0]);
        while(max!=min){
            long long int mid = (max+min)/2;
            long long int count = 0;
            int tmp = minus-1;      
            for(i=0;i<minus;i++){
                int flag=0; 
                for(j=tmp;j>i;j--){
                    if(A[i]*A[j]>mid){
                        flag++;
                        tmp=j+1;
                        count+=(minus-1-j);
                        break;
                    }
                }
                if(flag==0)count+=(minus-1-i);
                if(count>K)break;
            }
            tmp = N-1-plus;
            for(i=N-1;i>N-1-plus;i--){
                flag=0; 
                for(j=tmp;j<i;j++){
                    if(A[i]*A[j]>mid){
                        flag++;
                        tmp=j-1;
                        count+=(j-(N-1-plus));
                        break;
                    }
                }
                if(flag==0)count+=(i-(N-1-plus));
                if(count>K)break;
            }            
            if(count<=K)min=mid;
            if(count>=K)max=mid;
        }
        cout << mid << endl;
    }
    }

Compiler output:
a.cc: In function 'int main()':
a.cc:46:17: error: 'mid' was not declared in this scope; did you mean 'min'?
   46 |         cout << mid << endl;
      |                 ^~~
      |                 min
a.cc:52:32: error: 'max' cannot be used as a function
   52 |         long long int max = max(A[N-1]*A[N-1],A[0]*A[0]);
      |                             ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:72:17: error: 'flag' was not declared in this scope
   72 |                 flag=0;
      |                 ^~~~
a.cc:87:17: error: 'mid' was not declared in this scope; did you mean 'min'?
   87 |         cout << mid << endl;
      |                 ^~~
      |                 min