1. Prompt and guard. Read k; exit early if it is less than 1.
2. Same core. Only the source of k changes; the ternary replaces the if/else.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int k = sc.nextInt();
if (k < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Math.max Compact
Same shape — Math.max(i, j) replaces the if/else in both halves.
Java
public class ConcentricNumberSquareCompact {
public static void main(String[] args) {
int k = 5;
for (int i = k; i >= 1; i--) {
for (int j = k; j >= 1; j--)
System.out.print(Math.max(i, j) + " ");
for (int j = 2; j <= k; j++)
System.out.print(Math.max(i, j) + " ");
System.out.println();
}
}
}
1. Same structure. Left and right half-loops are unchanged.
2. Clearer rule.Math.max(i, j) is the same as j > i ? j : i.
3. Next step tip. Program 47 often mirrors rows below the center for a full concentric square.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of numbers
If println is inside either half-loop, each value lands on its own line. Use print for values; println only after both halves.
j from 1
Double center
Starting the right half at j = 1 reprints the center column and breaks the symmetry.
min instead
Different shape
Using Math.min(i, j) flips the rings — useful to explore, but not this pattern.
No println
One long line
Forgetting println() after the row glues every number onto one endless line.
k = 1
Single cell
Output is just 1 — the right half loop never runs.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Concentric square (Examples 1–3)
O(k²)
O(1)
k rows each print 2k − 1 values — still quadratic. Only loop counters are stored.
Remember
Key Takeaways
Rule: each cell prints max(i, j) — outer rings stay large.
Two halves: left j = k..1, right j = 2..k to avoid a double center.
Break the row: call println only after both half-loops.
Complexity:O(k²) time; O(1) extra space.
One line: for each i from k down to 1, print max(i, j) on the left half then the right half, then println().
Frequently Asked Questions
Because k=5 is the maximum. On any row i, column values near the edges use j when j>i, so the outer columns stay at k.
For row i and column j, the program prints j when j>i; otherwise i. That is equivalent to max(i, j).
When i=1, every j>1 prints j, but j=1 prints 1 at the center — creating the full symmetric sequence.
Yes. Replace k=5 with any positive integer. You get k rows and 2k-1 values per row.
This tutorial prints the top k rows (concentric-style). A full square often mirrors rows below the center too — see Program 47.
Yes. System.out.print(Math.max(i, j) + " ") works — see Example 3.
O(k²) because k rows each print about 2k-1 values.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Each cell prints max(i, j) — the left half loops j = k..1, the right half mirrors with j = 2..k. The smallest value appears at the center of the last row.