Given a linked list, whose nodes represent the coordinates of the cartesian plane. Find the minimum and the maximum slope of simultaneous points of coordinates.
Linked List : P1(X1, Y1) -> P2( X2, Y2) -> P3(X3, Y3).
Here P1(point1) corresponds to coordinates (X1, Y1), similarly P2(point2) corresponds to coordinates (X2, Y2).
Your task is to find the Maximum(Slope(P1, P2), Slope(P2, P3)) and Minimum(Slope(P1, P2), Slope(P2, P3)).
Note :You only need to return the starting node for minimum and maximum slope. So if slope(P1, P2) is maximum, just return P1.
In case of more than one possible solution return the first occurring solution.
The first line of input contains an integer 'T' representing the number of Test cases.
The first line of each test case contains linked list elements/nodes separated by space and terminated by -1. Each node would consist of 'X' and 'Y' coordinates.
Output Format:
For each test case, print the Maximum(Slope(P1, P2), Slope(P2, P3)) and Minimum(Slope(P1, P2), Slope(P2, P3)).
Note
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 10^3
-10^9 <= X <= 10^9
-10^9 <= Y <= 10^9
Where 'N' denotes the length of the linked list and 'X' and 'Y' denote the coordinates of the 'X' and 'Y' axis respectively.
Time Limit: 1 sec
2
1 5 7 9 6 3 -1
3 2 4 3 5 4 6 5 -1
1 5 7 9
3 2 3 2
Here we have 2 test cases, hence there are 2 linked lists.
Test Case 1: For the first linked list (1,5)->(7,9)->(6,3), the minimum slope is between nodes (1,5) and (7,9) so we print the first node i.e (1, 5) and the maximum slope is between nodes (7,9) and (6,3) so we print the first node i.e (7, 9).
Tese Case 2: For the second linked list (3,2)->(4,3)->(5,4)->(6,5), there are multiple answers, but we only need first occurring nodes. So the first minimum slope is between nodes (3,2) and (4,3) so we print the first node i.e (3, 2) and the first maximum slope is between nodes (3,2) and (4,3) so we print the first node i.e (3, 2).
2
4 3 8 6 2 9 4 18 -1
2 9 4 18 4 3 8 6 -1
8 6 2 9
4 3 4 18
Try to recursively traverse on each node and calculate the slope between consecutive nodes.
O(N), where N is the size of the Linked List.
We are recursively traversing through the linked list of size N, for N nodes time complexity will be O(N).
O(N), where N is the size of the Linked List.
As we are recursively traversing the linked list, stack space for recursive calls of N nodes of the linked list will be in the order of N. Hence the space complexity will be O(N).