The function accepts the coordinates of truck A(Ax, Ay) int Distance

You are given a function,
int Distance(int Ax, int Ay, int Bx, int By);
The function accepts the coordinates of truck A(Ax, Ay) and the coordinates of truck B(Bx, By), as input. The office is at location (0, 0). Implement the function to determine which truck is closer to the office by comparing the square of their distances. Return the square of the distance of the truck which is closer to the office.
Square of distance between two points having coordinates (x, y) and (0, 0):
(Distance)2 = [x2 + y2]
Note:

  • No need to compute the square root of the distances of trucks A and B from the office.
  • If both trucks are at equal distance, return square of distance of either truck.

Example:
Input:
12
5
12
9
Output:
169
Explanation:
(Distance of truck A)2 = [Ax2 + Ay2] = [122 + 52] = (144 + 25) = 169
(Distance of truck B)2 = [Bx2 + By2] = [122 + 92] = (144 + 81) = 225
Since, (169 < 225), hence the output is 169.

Sample input

6
8
3
4

Sample Output

25

Instructions :

  • This is a template based question, DO NOT write the “main” function.
  • Your code is judged by an automated system, do not write any additional welcome/greeting messages.
  • “Save and Test” only checks for basic test cases, more rigorous cases will be used to judge your code while scoring.
  • Additional score will be given for writing optimized code both in terms of memory and execution time.

Solution

def Distance(Ax, Ay, Bx, By):
    a = (Ax**2) + (Ay**2)
    b = (Bx**2) + (By**2)
    if a < b:
        return a
    else:
        return b

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *