Check if given four integers (or sides) make rectangle in Python

A rectangle is a quadrilateral with four right angles and two pairs of opposite sides that are equal in length. Given four integers representing sides, we need to check if they can form a rectangle.

So, if the input is like sides = [10, 30, 30, 10], then the output will be True as there are two pairs of equal sides (10, 10) and (30, 30).

Algorithm

To solve this, we will follow these steps −

  • If all four sides are equal, then it's a square (which is also a rectangle), return True
  • Check all possible pairings of sides to see if we have exactly two pairs of equal sides
  • Return False if no valid pairing is found

Method 1: Using Multiple Conditions

Check all possible arrangements of sides to find two pairs of equal values −

def solve(sides):
    if sides[0] == sides[1] == sides[2] == sides[3]:
        return True
    elif sides[0] == sides[1] and sides[2] == sides[3]:
        return True
    elif sides[0] == sides[3] and sides[2] == sides[1]:
        return True
    elif sides[0] == sides[2] and sides[3] == sides[1]:
        return True
    return False

sides = [10, 30, 30, 10]
print(solve(sides))
True

Method 2: Using Counter (More Elegant)

Count the frequency of each side value. For a rectangle, we should have exactly two unique values, each appearing twice −

from collections import Counter

def solve(sides):
    count = Counter(sides)
    # Rectangle: either all sides equal OR exactly 2 unique values each appearing twice
    if len(count) == 1:
        return True  # Square case
    elif len(count) == 2 and all(freq == 2 for freq in count.values()):
        return True  # Rectangle case
    return False

# Test with different examples
test_cases = [
    [10, 30, 30, 10],  # Rectangle
    [5, 5, 5, 5],      # Square
    [10, 20, 30, 40],  # No pairs
    [15, 15, 25, 20]   # Only one pair
]

for sides in test_cases:
    print(f"Sides {sides}: {solve(sides)}")
Sides [10, 30, 30, 10]: True
Sides [5, 5, 5, 5]: True
Sides [10, 20, 30, 40]: False
Sides [15, 15, 25, 20]: False

Method 3: Using Sorting

Sort the sides and check if first two elements are equal and last two elements are equal −

def solve(sides):
    sorted_sides = sorted(sides)
    return sorted_sides[0] == sorted_sides[1] and sorted_sides[2] == sorted_sides[3]

sides = [10, 30, 30, 10]
print(solve(sides))

# Test edge cases
print(solve([5, 5, 5, 5]))    # Square
print(solve([1, 2, 3, 4]))    # No rectangle
True
True
False

Comparison

Method Time Complexity Space Complexity Readability
Multiple Conditions O(1) O(1) Low
Counter O(n) O(n) High
Sorting O(n log n) O(1) High

Conclusion

The sorting approach is the most elegant and readable solution for checking if four sides can form a rectangle. For performance-critical applications with many calls, the multiple conditions method offers O(1) time complexity.

Updated on: 2026-03-25T15:05:54+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements