Find Four points such that they form a square whose sides are parallel to x and y axes in Python

Finding four points that form a square with sides parallel to the coordinate axes is a geometric problem. We need to identify diagonal points first, then verify if the other two corner points exist in our point set.

Problem Approach

To form a square with sides parallel to x and y axes, we need four points that satisfy these conditions ?

  • Two points should form a diagonal of the square

  • The diagonal points must have equal x-distance and y-distance

  • The other two corner points must exist in our point set

  • If multiple squares exist, select the one with maximum area

Algorithm Steps

The solution follows these key steps ?

  • Store all points in a dictionary for O(1) lookup

  • For each pair of points, check if they can form a diagonal

  • Verify if the remaining two corner points exist

  • Track the square with maximum side length

Implementation

def get_square_points(points, n):
    point_map = dict()
    
    # Store all points with their frequency
    for i in range(n):
        point_map[(points[i][0], points[i][1])] = point_map.get((points[i][0], points[i][1]), 0) + 1
    
    side = -1
    x = -1 
    y = -1
    
    # Check all pairs of points for potential diagonal
    for i in range(n):
        point_map[(points[i][0], points[i][1])] -= 1
        
        for j in range(n):
            point_map[(points[j][0], points[j][1])] -= 1
            
            # Check if points can form diagonal (equal x and y distances)
            if (i != j and abs(points[i][0] - points[j][0]) == abs(points[i][1] - points[j][1])):
                
                # Check if other two corners exist
                corner1 = (points[i][0], points[j][1])
                corner2 = (points[j][0], points[i][1])
                
                if point_map.get(corner1, 0) > 0 and point_map.get(corner2, 0) > 0:
                    current_side = abs(points[i][0] - points[j][0])
                    
                    # Update if larger square found or same size with smaller starting point
                    if (side < current_side or 
                        (side == current_side and 
                         (points[i][0] * points[i][0] + points[i][1] * points[i][1]) < (x * x + y * y))):
                        x = points[i][0]
                        y = points[i][1] 
                        side = current_side
            
            point_map[(points[j][0], points[j][1])] += 1
        
        point_map[(points[i][0], points[i][1])] += 1
    
    # Display results
    if side != -1:
        print("Side:", side)
        print("Points:", (x, y), (x + side, y), (x, y + side), (x + side, y + side))
    else:
        print("No such square")

# Test the function
n = 6
points = [(2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)]
get_square_points(points, n)
Side: 3
Points: (2, 2) (5, 2) (2, 5) (5, 5)

How It Works

The algorithm works by checking each pair of points as potential diagonal corners. For points (x1, y1) and (x2, y2) to form a diagonal ?

  • The condition |x1 - x2| = |y1 - y2| must be satisfied

  • The other two corners at (x1, y2) and (x2, y1) must exist

  • This forms a square with side length |x1 - x2|

Time and Space Complexity

Aspect Complexity Explanation
Time O(n²) Checking all pairs of points
Space O(n) Dictionary to store point frequencies

Conclusion

This solution efficiently finds the largest square by checking diagonal pairs and verifying corner existence. The algorithm handles duplicate points and selects the square with maximum area when multiple solutions exist.

Updated on: 2026-03-25T09:38:41+05:30

629 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements