![]() |
VOOZH | about |
Given a rectangle [(x1, y1), (x2, y2)] denoting the coordinates of bottom-left corner and top-right corner whose sides are parallel to coordinates axes and N points on its perimeter (at least one on each side). The task is to maximize the area of a triangle formed by these points.
Examples:
Input: rectangle[][]= {{0, 0}, {6, 6}},
👁 Image
coordinates[][] = {{0, 2}, {0, 3}, {0, 5}, {2, 0}, {3, 0}, {6, 0}, {6, 4}, {1, 6}, {6, 6}}
Output: 18
Explanation: Refer to the image below for explanation.
Approach: For finding the maximum area triangle by coordinates on a given rectangle, find the coordinates on each side which are most distant apart. So, suppose there are four sides a, b, c, d where a and c being the length of the rectangle and b, d being the breadth. Now the maximum area will be
MAX ( length * ( distance between farthest coordinates on either of breadth) / 2, breadth * ( distance between farthest coordinates on either of length) / 2 ).
Follow the steps below to solve the given problem.
Below is the implementation of the above approach.
18
Time complexity: O(N), Where N is the number of coordinates given.
Auxiliary Space: O(1)