In this HackerEarth K Devices problem solution, You are given the location of N devices on a coordinate plane and an integer K. Location of the ith device is donated by (Xi, Yi). A modem is located at (0,0). The range of modem is circular. All the devices within the range of the modem will connect to the modem. You have to find the minimum integral radius of the circular range of the modem such that at least K devices will connect to the modem.


HackerEarth K Devices problem solution


HackerEarth K Devices problem solution.

#include <bits/stdc++.h>

using namespace std;
const int MAX = 1e5 + 5;
long long x[MAX], y[MAX], dis[MAX];

int main(int argc, char* argv[])
{
    if(argc == 2 or argc == 3) freopen(argv[1], "r", stdin);
    if(argc == 3) freopen(argv[2], "w", stdout);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> y[i];
    }
    for (int i = 0; i < n; i++) {
        dis[i] = x[i]*x[i] + y[i]*y[i];
    }
    sort(dis, dis + n);
    cout << ceil(sqrt(dis[k-1])) << endl;
    return 0;
}