K Devices

You are given the location of N devices on a coordinate plane and an integer K. Location of 

ith the 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.

Example:

Input:  n = 3, k = 3, x = [1,-1,1],  y = [1,-1,-1]
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long kDevices(long long nlong long k,
                   long long x[], long long y[])
{
    vector<long longv;
    for (long long i = 0i < ni++)
    {
        long long p = (x[i] * x[i] + y[i] * y[i]);
        double q = sqrt(p);
        q = ceil(q);
        v.push_back(q);
    }
    sort(v.begin(), v.end());

    return v[k - 1];
}
int main()
{
    long long n = 3k = 3;

    long long x[n] = {1, -11};
    long long y[n] = {1, -1, -1};

    cout << kDevices(nkxy<< "\n";

    return 0;
}


No comments:

Post a Comment