Java ProgramsPatternsPrint 0-1 Triangle

Print 0-1 Triangle in Java

beginner·  Patterns  ·  Number Patterns

Problem

A 0-1 triangle alternates its value based on both the row and column together, so consecutive positions — across a row and down a column — never share the same value.

Given a number of rows, print a triangle where the entry at row i, column j is (i + j) modulo 2.

Input
n = 5
Output
0 1 0 0 1 0 1 0 1 0 0 1 0 1 0

Java Program

Java
public class ZeroOneTriangle { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { StringBuilder line = new StringBuilder(); for (int j = 1; j <= i; j++) { if (j > 1) line.append(" "); line.append((i + j) % 2); // flips whenever either i or j changes } System.out.println(line); } } }

Output

0 1 0 0 1 0 1 0 1 0 0 1 0 1 0

Core Logic

Adding the row and column positions together before taking the result modulo 2 flips the printed value every time either one changes, producing a genuine checkerboard instead of a row-by-row alternation.

How It Works
  1. 1The outer loop runs i from 1 to n, and the inner loop runs j from 1 to i, visiting every position.
  2. 2(i + j) % 2 is computed fresh at each position — since either i or j changing by one flips whether the sum is even or odd, the printed value flips too.
  3. 3Moving one step to the right within a row flips the value, and moving one row down at the same column also flips it — that's what makes this a checkerboard rather than the row-only alternation of a binary triangle.
  4. 4No stored state is needed between positions — each cell's value is computed independently from just its own i and j.
For n = 5, row 3 (i = 3) prints 0 1 0 — position (3,1) sums to 4 (even, 0), (3,2) sums to 5 (odd, 1), (3,3) sums to 6 (even, 0).
💡

Key Point: This is the direct counterpart to the binary triangle — that one alternates only between rows, this one alternates between both rows and columns, at every single position.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: Every position in the triangular grid is visited once to compute its own value directly, a total proportional to n², with no stored state carried between cells.

Key Concepts

nested for loopmodulo operatorcheckerboard pattern

Approach 2: Java 8

Java
import java.util.stream.Collectors; import java.util.stream.IntStream; public class ZeroOneTriangleStream { public static void main(String[] args) { int n = 5; IntStream.rangeClosed(1, n) .mapToObj(i -> IntStream.rangeClosed(1, i) .mapToObj(j -> String.valueOf((i + j) % 2)) .collect(Collectors.joining(" "))) .forEach(System.out::println); } }

Output

0 1 0 0 1 0 1 0 1 0 0 1 0 1 0

Core Logic

Since each position's value depends only on its own row and column, mapping every column index directly to (i + j) % 2 reproduces the checkerboard without any state carried between positions.

How It Works
  1. 1IntStream.rangeClosed(1, n) generates one stream element per row i.
  2. 2For each row, an inner IntStream.rangeClosed(1, i) generates that row's column positions j.
  3. 3.mapToObj(j -> String.valueOf((i + j) % 2)) computes each position's value directly from its own i and j, the same formula the loop version uses.
  4. 4Collectors.joining(" ") joins that row's values before forEach prints it.
For row 3, the inner stream maps j = 1, 2, 3 to (3+1)%2, (3+2)%2, (3+3)%2, giving "0 1 0".
💡

Key Point: Because (i + j) % 2 reads only its own two parameters, this maps cleanly onto a stream even though the value depends on both dimensions at once — no shared counter or lookback into a previous cell is needed.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The nested streams still compute one value per grid position across all rows, proportional to n², without collecting the full triangle.

Key Concepts

StreamIntStream.rangeClosed()Collectors.joining()

Related Programs