Program to find out if k monitoring stations are enough to monitor particular points in Python


Suppose there is a sensor module that can monitor its nearby environment up to a radius of r. There are some things in the lattice point of the module's monitoring circle that needs to be monitored. So, k number of low-powered modules are placed so that they can monitor only those specific points. Given the square of the radius and k number of low-powered modules, we shall have to find out if the points can be monitored correctly. We return true if monitoring is possible, otherwise, we return false.

So, if the input is like square of radius (j) = 4, number of monitoring points(k) = 3, then the output will be False

If j = 4, there are 4 points on the circumference of the monitoring circle; that are: (0,2), (0,-2), (2,0), and(-2,0). So, if we introduce three new monitoring stations, we cannot monitor the points fully.

To solve this, we will follow these steps −

  • square_set := a set containing square of values up to 44721
  • i := 0
  • res := 0
  • while i <(j ^ 0.5), do
    • if (j - i ^ 2) is present in square_set, then
      • res := res + 1
    • i := i + 1
  • res := res * 4
  • if k >= res, then
    • return True
  • otherwise,
    • return False

Example

Let us see the following implementation to get better understanding −

square_set = set([z ** 2 for z in range(44722)])
def solve(j, k):
    i = 0
    res = 0
    while i < (j ** 0.5):
        if j - i ** 2 in square_set:
            res += 1
        i += 1    
    res *= 4
    if k >= res:
        return True
    else:
        return False

print(solve(4, 3))

Input

4, 3

Output

False

Updated on: 06-Oct-2021

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements