The alternating X and 0 rectangles pattern fills a grid with nested rectangular rings that take turns: X on the border, 0 on the next ring, X again inside that. It is a well-known GeeksforGeeks matrix problem.
Quick answer: For each cell, take the distance to the nearest edge: the smallest of i, j, rows β 1 β i and cols β 1 β j. Print X when that distance is even and 0 when it is odd. Every ring has one distance, so the rings alternate automatically.
This program is part of the Spiral Patterns collection.
Table of contents8 sections
Alternating X and 0 Rectangles Pattern output
Output (5 rows, 6 columns):
X X X X X X X 0 0 0 0 X X 0 X X 0 X X 0 0 0 0 X X X X X X X

How the alternating x and 0 rectangles pattern works
The same distance-to-edge idea used for concentric squares works for rectangles, because it only looks at how far a cell is from each side. The outer ring is distance 0, the next one distance 1, and so on, so the parity of the distance decides the character. Unlike a square, a rectangle’s innermost ring is a line or a small block, which you can see in the middle row of the output.
- For each row
iand columnj(counted from 0), setdto the smallest ofi,j,rows β 1 β iandcols β 1 β j. - Print X if
dis even, otherwise 0, each followed by a space. - Move to the next line after each row.
Key takeaways
- The distance to the nearest edge labels each ring.
- Even rings are X and odd rings are 0.
- The same code works for any rectangle size.
- The innermost ring of a rectangle can be a single line of cells.
Alternating X and 0 Rectangles Pattern in C
#include <stdio.h>
int main(void) {
int rows = 5, cols = 6;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int d = i; /* distance to the nearest edge */
if (j < d) d = j;
if (rows - 1 - i < d) d = rows - 1 - i;
if (cols - 1 - j < d) d = cols - 1 - j;
printf(d % 2 == 0 ? "X " : "0 ");
}
printf("\n");
}
return 0;
}Alternating X and 0 Rectangles Pattern in C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int rows = 5, cols = 6;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int d = min(min(i, j), min(rows - 1 - i, cols - 1 - j)); // distance to the nearest edge
cout << (d % 2 == 0 ? "X " : "0 ");
}
cout << endl;
}
return 0;
}Alternating X and 0 Rectangles Pattern in Java
public class AlternatingXORectangles {
public static void main(String[] args) {
int rows = 5, cols = 6;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int d = Math.min(Math.min(i, j), Math.min(rows - 1 - i, cols - 1 - j)); // distance to the nearest edge
System.out.print(d % 2 == 0 ? "X " : "0 ");
}
System.out.println();
}
}
}Alternating X and 0 Rectangles Pattern in Python
rows, cols = 5, 6
for i in range(rows):
for j in range(cols):
d = min(i, j, rows - 1 - i, cols - 1 - j) # distance to the nearest edge
print("X" if d % 2 == 0 else "0", end=" ")
print()Time and space complexity
Each cell takes constant work, so the program runs in O(rows Γ cols) time with O(1) extra memory.
Frequently asked questions
Does it work for a square?
Yes. Set rows and cols to the same value and the rings become squares.
Can I build it by filling rings one by one instead?
Yes: fill the border with X, then the next ring with 0, moving four boundaries inwards each time, the way the spiral programs do. The distance formula avoids the extra loops.
Why is the middle row X X in the centre?
With 5 rows and 6 columns the innermost ring has distance 2, which is even, and it is only two cells long.






