Given the coordinates of 5 2-dimensional points, check if they form a closed T shape. Print ‘Yes’ if they form T shape and ‘No’ otherwise. Note: Coordinates should be distinct and integers.
There are 4-types of T shaped formations possible according to the given conditions:
Input: [[7, 5], [8, 5], [6, 5], [7, 7], [7, 6]] Output: Yes Input: [[0, 0], [1, 0], [2, 0], [1, -1], [1, -2]] Output: Yes
- Consider the first point in the given list as the center (x, y) (i.e intersection of the two lines that form a T-shape).
- Then check if all the points which are needed to form a T-shape of which (x, y) are the center are present in the list of given points or not.
- Check this for all 4 possible patterns of T-shape.
Below is the implementation of the above approach:
Time Complexity: O(1)