Program to check whether given list of blocks are symmetric over x = y line or not in python

Suppose we have a list of numbers called nums representing the height of square blocks. We need to check whether the shape is symmetric over the y = x line (diagonal reflection).

For a shape to be symmetric over the y = x line, when we reflect it diagonally, the resulting shape should be identical to the original.

Problem Understanding

Given nums = [7, 5, 3, 2, 2, 1, 1], we need to verify if this block arrangement is symmetric over the y = x line.

y = x 7 5 3 Y X

Algorithm

We use two pointers approach to check symmetry ?

  • i := 0 (left pointer)
  • j := size of nums - 1 (right pointer)
  • For each column from right, check if blocks match the expected symmetric pattern
  • If any mismatch found, return False
  • Otherwise, return True

Implementation

class Solution:
    def solve(self, nums):
        i = 0
        j = len(nums) - 1
        
        while i <= j:
            h = nums[j]  # Height of current column from right
            
            # Check if blocks match symmetric pattern
            while i < h:
                if nums[i] != j + 1:
                    return False
                i += 1
            j -= 1
        
        return True

# Test the solution
ob = Solution()
nums = [7, 5, 3, 2, 2, 1, 1]
print("Input:", nums)
print("Is symmetric over y = x line:", ob.solve(nums))
Input: [7, 5, 3, 2, 2, 1, 1]
Is symmetric over y = x line: True

How It Works

The algorithm works by checking each column from right to left ?

# Example walkthrough with nums = [7, 5, 3, 2, 2, 1, 1]
nums = [7, 5, 3, 2, 2, 1, 1]

# Step-by-step execution
i, j = 0, 6  # Start from leftmost and rightmost
print(f"Column {j}: height = {nums[j]} = 1")
print(f"Checking if nums[{i}] == {j+1}: {nums[i]} == 7? {nums[i] == 7}")

# Move to next column
i, j = 1, 5
print(f"Column {j}: height = {nums[j]} = 1") 
print(f"Checking if nums[{i}] == {j+1}: {nums[i]} == 6? {nums[i] == 6}")
Column 6: height = 1 = 1
Checking if nums[0] == 7: 7 == 7? True
Column 5: height = 1 = 1
Checking if nums[1] == 6: 5 == 6? False

Testing with Different Cases

class Solution:
    def solve(self, nums):
        i = 0
        j = len(nums) - 1
        
        while i <= j:
            h = nums[j]
            while i < h:
                if nums[i] != j + 1:
                    return False
                i += 1
            j -= 1
        return True

ob = Solution()

# Test cases
test_cases = [
    [7, 5, 3, 2, 2, 1, 1],
    [1, 2, 3],
    [3, 2, 1],
    [1, 1, 1]
]

for case in test_cases:
    result = ob.solve(case)
    print(f"nums = {case} ? Symmetric: {result}")
nums = [7, 5, 3, 2, 2, 1, 1] ? Symmetric: True
nums = [1, 2, 3] ? Symmetric: False
nums = [3, 2, 1] ? Symmetric: True
nums = [1, 1, 1] ? Symmetric: True

Conclusion

This algorithm efficiently checks diagonal symmetry using a two-pointer approach. The key insight is that for y = x symmetry, each column's height must match specific positions when reflected diagonally.

Updated on: 2026-03-25T12:51:12+05:30

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements