Weather Observation Station 18 | Hackerrank

Author: neptune | 18th-Apr-2023
🏷️ #SQL #Hackerrank

Problem Statement:

Consider P1(a,b) and P2(c,d) two points on a 2D plane.

  •  ‘a’ happens to equal the minimum value in Northern Latitude (LAT_N in STATION).

  •  ‘b’ happens to equal the minimum value in Western Longitude (LONG_W in STATION).

  •  ‘c’ happens to equal the maximum value in Northern Latitude (LAT_N in STATION).

  •  ‘d’ happens to equal the maximum value in Western Longitude (LONG_W in STATION).

Query the Manhattan Distance between points P1 and P2 and round it to a scale of 4 decimal places.


Manhattan Distance: The distance between two points measured along axes at right angles. In a plane with p1 at (x1, y1) and p2 at (x2, y2), it is |x1 - x2| + |y1 - y2|.

Input Format:

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.


Sample Input:

Assume the STATION table has the following data:

ID

CITY

STATE

LAT_N

LONG_W

1

A

X

10.000

20.000

2

B

Y

12.000

22.000

3

C

Z

8.000

18.000

4

D

W

14.000

24.000


Sample Output:

The Manhattan Distance between the two points is computed as:


 ABS(14.000 - 8.000) + ABS(24.000 - 18.000) = 12.000



Explanation:

The solution applies the concept of Manhattan Distance to find the distance between two points on a 2D plane. 

Manhattan Distance is the sum of the absolute differences of the coordinates of two points. It is calculated by adding the absolute differences between the x-coordinates and y-coordinates of the two points.


In this problem, we need to find the Manhattan Distance between two points P1(a,b) and P2(c,d) where:

The SQL query written uses the MIN and MAX functions to retrieve the minimum and maximum values of latitude and longitude from the STATION table. 

The ABS function is then used to calculate the absolute differences between the latitude and longitude values of the two points, and the ROUND function is used to round the result to 4 decimal places.


Solution:

MySQL Query:

 

 SELECT

 ROUND(

    ABS(

       (SELECT MIN(LAT_N) FROM STATION) -

       (SELECT MAX(LAT_N) FROM STATION)

       )

    +

    ABS(

       (SELECT MIN(LONG_W) FROM STATION) -

       (SELECT MAX(LONG_W) FROM STATION)

       ),

    4);



The output of the query is the Manhattan Distance between the two points, rounded to 4 decimal places.

Execution:


I hope this works for you. Let me know if you face any issue in the comment section.



👉 Read More
The Blunder | Hackerrank
5. Solution of Hacker Rank Weather Observation Station 8.
7.Solution of Hacker Rank The Report
Identifying the Odd One Out in a Series of Strings | Hackerrank
4. Solution of Hacker Rank Weather Observation Station 6.
3. Solution of Hacker Rank Weather Observation Station 4.
The PADS | Hackerrank
6. Solution of Hacker Rank Employee Salaries.
1. Basic SQL Select Query of Hacker Rank.
Generate Fibonacci Sequence - JavaScript | Hackerank
Team Formation Hackerrank | Julia
Modified 0-1 knapsack problem | Frsco Play Hackerrank
2. Solution of Hacker Rank Weather Observation Station 3.
Solving the Ice Cream Parlor Problem | Hackerrank
AngularJS - Know Tech Frameworks | Fresco Play
PySpark Milestone Black Friday Sales Data | Fresco Play Hackerrank
30+ SQL Interview Questions
Backspace String Compare using R | Fresco Play
Top Earners | HackerRank
Python - Number Based Problem | Hackerrank
Explore more Blogs...