Check if any square (with one colored cell) can be divided into two equal parts in Python

In this article, we are given a square of size n×n with exactly one cell colored. The task is to determine whether this square can be divided into two equal parts by making a single straight cut, ensuring that the colored cell lies entirely within one of the parts.

Two equal parts means both splits must contain the same number of cells, which is only possible if the square side length is even. We need to check if the position of the colored cell allows such a split without being intersected by the cut.

Understanding the Problem

For a square to be divided into two equal parts:

  • The side length n must be even
  • We can make either a horizontal cut (between rows n/2 and n/2+1) or a vertical cut (between columns n/2 and n/2+1)
  • The colored cell must not lie on the cutting line
Horizontal Cut Colored Vertical Cut Colored

Examples

Scenario 1: Valid Split

<b>Input:</b>
n = 4, a = 2, b = 3

<b>Output:</b> YES

<b>Explanation:</b>
The 4×4 square can be divided equally. A horizontal cut between rows 2 and 3 creates two equal parts of 8 cells each. The colored cell at (2,3) is in the upper half, so the cut doesn't intersect it.

Scenario 2: Invalid Split

<b>Input:</b>
n = 5, a = 3, b = 2

<b>Output:</b> NO

<b>Explanation:</b>
The 5×5 square has an odd side length, making equal division impossible since each half would have a fractional number of cells.

Solution Implementation

def can_divide_square(n, a, b):
    # Check if the square can be divided equally
    if n % 2 != 0:
        return "NO"
    
    mid = n // 2
    
    # Check if colored cell is not on the middle row or column
    # If it's not on the cutting lines, we can make a valid split
    if a != mid and a != mid + 1:
        return "YES"  # Horizontal cut possible
    
    if b != mid and b != mid + 1:
        return "YES"  # Vertical cut possible
    
    return "NO"

# Test the examples
print(can_divide_square(4, 2, 3))
print(can_divide_square(5, 3, 2))
print(can_divide_square(4, 2, 2))  # Additional test
YES
NO
YES

Algorithm Explanation

The algorithm works as follows:

  1. Check divisibility: If n is odd, return "NO" immediately
  2. Find middle position: Calculate mid = n // 2
  3. Check cut possibilities:
    • Horizontal cut: between rows mid and mid + 1
    • Vertical cut: between columns mid and mid + 1
  4. Validate position: If the colored cell doesn't lie on either cutting line, return "YES"

Time and Space Complexity

Complexity Value Reason
Time O(1) Only arithmetic operations
Space O(1) No additional data structures

Conclusion

A square can be divided into two equal parts only if its side length is even and the colored cell doesn't lie on the cutting line. The solution runs in constant time by checking the position relative to the middle row and column.

Updated on: 2026-03-25T14:20:53+05:30

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements