A symmetric decreasing alphabet square prints fixed-width mirrored rows whose interior floor drops from the top letter down to a single center A.
Remember
Rule: for floor i from k down to 0,
left j = k..0 and right j = 1..k;
print j if j > i, else print i
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E ← top = 'E' (k = 4, width 9)
The right half starts at j = 1 (B) so the center A is not duplicated. Program 29 reuses this row rule in two phases to form a full diamond.
Approach
How to Solve It
Two ways to emit the same square — inline floor checks, then optionally a shared helper for both halves.
Method
Idea
Best for
Inline j > i
Left k..0 + right 1..k with the same ternary
Learning, interviews, exams
Helper method
One printCell owns the floor rule
Cleaner demos once the rule clicks
Pseudocode
Pseudocode
k = top - 'A'
for i from k down to 0:
for j from k down to 0:
print (j > i ? alpha[j] : alpha[i])
for j from 1 to k:
print (j > i ? alpha[j] : alpha[i])
print newline
Cheat sheet
Goal
Pattern
Top index
int k = top - 'A'; (4 for E)
Drop the floor
for (int i = k; i >= 0; i--)
Left half
for (int j = k; j >= 0; j--) + j > i ? alpha[j] : alpha[i]
Right half
for (int j = 1; j <= k; j++) (skip 0)
End the row
System.out.println();
Row width
2 * k + 1 (9 for A..E)
Avoid double A
Start the right loop at 1, not 0
Printing Letters vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each letter (and its trailing space)
System.out.println
Ends the current line
After both half-loops
Print cells without a newline, then end the row once.
Try it
Live Preview
Change the top letter and the layered square updates instantly — width is always 2k + 1.
One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.
Live resultTop E · 5 rows · width 9
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
Trace
Worked Walkthrough — Top = E (k = 4)
Trace each row floor and the resulting 9-letter line.
i
Floor letter
Printed row
4
E
E E E E E E E E E
3
D
E D D D D D D D E
2
C
E D C C C C C D E
1
B
E D C B B B C D E
0
A
E D C B A B C D E
Width is always 2×4 + 1 = 9. The last row is the full palindrome around a single A. Total cells: 5 × 9 = 45.
Code
Java Programs
Three complete programs: fixed A–E, top-letter input, and a helper-method rewrite. Use View Output to reveal sample results.
Example 1 — Fixed A–E
Two symmetric scans per row with the same j > i check.
Java
public class SymmetricAlphabetSquare {
public static void main(String[] args) {
int k = 4; // index for 'E'
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = k; i >= 0; i--) {
for (int j = k; j >= 0; j--)
System.out.print(j > i ? alpha[j] + " " : alpha[i] + " ");
for (int j = 1; j <= k; j++)
System.out.print(j > i ? alpha[j] + " " : alpha[i] + " ");
System.out.println();
}
}
}
Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
How It Works
1. Fix the top index.k = 4 means the highest letter is alpha[4] = E.
2. Drop the floor. Outer i runs from k down to 0 — one interior floor per row.
3. Left half. Scan j from k down to 0; print alpha[j] when j > i, else alpha[i].
4. Right half, then break. Scan j from 1 to k with the same rule, then println.
When i = 2 (floor C), columns where j > 2 print E/D borders, and interior cells print C.
Example 2 — Top Letter Input
Works for A..top with the same symmetric square. Prefer validating a single A–Z character in real apps.
Java
import java.util.Scanner;
public class SymmetricAlphabetSquareInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter top letter (like E): ");
char top = sc.next().toUpperCase().charAt(0);
int k = top - 'A';
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = k; i >= 0; i--) {
for (int j = k; j >= 0; j--)
System.out.print(j > i ? alpha[j] + " " : alpha[i] + " ");
for (int j = 1; j <= k; j++)
System.out.print(j > i ? alpha[j] + " " : alpha[i] + " ");
System.out.println();
}
sc.close();
}
}
Output (when user enters C)
Enter top letter (like E): C
C C C C C
C B B B C
C B A B C
How It Works
1. Prompt and scale.k = top - 'A' sets both the floor loop and the two halves.
2. Same square core. Width becomes 2k + 1 (5 letters for top = C).
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNext()) {
System.out.println("Enter one letter from A to Z.");
return;
}
String raw = sc.next().trim().toUpperCase();
if (raw.length() != 1 || raw.charAt(0) < 'A' || raw.charAt(0) > 'Z') {
System.out.println("Enter one letter from A to Z.");
return;
}
char top = raw.charAt(0);
Example 3 — Helper Method
Often clearer to read: one method applies the floor rule so left and right loops stay thin.
Java
public class SymmetricAlphabetSquareHelper {
static void printCell(char[] alpha, int j, int i) {
System.out.print(j > i ? alpha[j] + " " : alpha[i] + " ");
}
public static void main(String[] args) {
int k = 4;
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = k; i >= 0; i--) {
for (int j = k; j >= 0; j--)
printCell(alpha, j, i);
for (int j = 1; j <= k; j++)
printCell(alpha, j, i);
System.out.println();
}
}
}
Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
How It Works
1. Own the rule once.printCell applies j > i in a single place.
2. Thin loops. Left and right loops only decide which columns to visit.
3. Same shape. Output matches Examples 1–2 — useful when you want to explain the floor rule separately.
Edge Cases & Pitfalls
Check these before calling the solution done.
j = 0
Duplicate center A
If the right half starts at 0, the last row prints A twice. Keep for (j = 1; j <= k; j++).
j >= i
Wrong borders
Using j >= i changes which cells belong to the floor vs the border. Stick to strict j > i.
println inside
Column of letters
If println is inside either half-loop, each cell lands on its own line. Use print for cells; println only after both halves.
top = A
Single A
When k = 0, output is just A — right half never runs. A good sanity check.
top < A
Empty / invalid
Validate A–Z before computing k.
Bad input
Validate one letter
Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.
Analysis
Time and Space Complexity
Program
Time
Extra space
Inline loops (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet table)
Helper method (Example 3)
O(n²)
O(1)
For n = k + 1 rows of width 2k + 1, total cells = n × (2n - 1) — still quadratic in n.
Remember
Key Takeaways
Rule: for floor i, print j > i ? alpha[j] : alpha[i] on both halves.
Mirror cleanly: left k..0, right 1..k — skip duplicating center A.
Break the row: call println only after both half-loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each floor i from top down to A, scan left k..0 and right 1..k, printing j > i ? alpha[j] : alpha[i], then println().
Frequently Asked Questions
It prints the border letters when the column letter j is above the current row floor i; otherwise it prints i. This builds higher-letter borders with a flat interior.
The first loop scans E down to A. The second scans B up to E so the middle A is printed once and the row is mirrored.
For letters A..k, width is (k-A+1) + (k-A) = 2*(k-A)+1. For A..E, width is 9.
Pick a larger top letter (like H), set k = top - 'A', and keep the same loop structure.
O(n²) for n letters because there are n rows and each row prints O(n) cells.
Starting at 0 would print alpha[0] (A) again and duplicate the center. Starting at 1 (B) mirrors the left half cleanly.
System.out.print stays on the same line. System.out.println ends the current line. Letters use print; the row break uses println after both half-loops.
Use Scanner next(), take charAt(0) after toUpperCase(), require A–Z, and reject empty or multi-character tokens.
🤔
Did you know?
Fix k at the top letter (E). Outer loop i goes from k down to A. Left half scans j = k..A; right half scans j = B..k so A appears once in the middle. Each position prints j when j > i, otherwise prints i.