Follows the X of stars and zeros in Program 45; next is the full concentric square in Program 47.
Approach
How to Solve It
One outer layer loop, then left and right halves with the same cell rule.
Method
Idea
Best for
Two-half row
Left k..1, right 2..k, value j if j > i else i
Learning, interviews, exams
Input k
Same logic with a user-chosen peak
Practice / demos
Pseudocode
Pseudocode
for i from k down to 1:
for j from k down to 1:
print (j if j > i else i) and a space
for j from 2 to k:
print (j if j > i else i) and a space
print newline
Cheat sheet
Goal
Pattern
Layer loop
for i in range(k, 0, -1):
Left half
for j in range(k, 0, -1):
Right half
for j in range(2, k + 1):
Cell value
print(j if j > i else i, end=" ")
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(..., end=" ")
Stays on the same line
Each number plus a trailing space
print()
Ends the current line
After both half-loops finish a row
Print numbers without a newline, then end the row once.
Try it
Live Preview
Change k and the concentric square updates instantly.
Whole numbers from 1 to 9. Width = 2k − 1. Tap a chip or type a value — the preview redraws as you go.
Trace each layer: left half, then mirrored right half.
i
Left j=3..1
Right j=2..3
Printed row
3
3 3 3
3 3
3 3 3 3 3
2
3 2 2
2 3
3 2 2 2 3
1
3 2 1
2 3
3 2 1 2 3
Rows = k, columns = 2*k − 1. Total cells for k = 5 = 5 × 9 = 45.
Code
Python Programs
Three complete programs: fixed k = 5, input()k, and a compact k = 3 demo. Use View Output for sample results.
Example 1 — Fixed k = 5
If-else cell rule with explicit left and right half-loops.
Python
k = 5
for i in range(k, 0, -1):
for j in range(k, 0, -1):
if j > i:
print(j, end=" ")
else:
print(i, end=" ")
for j in range(2, k + 1):
if j > i:
print(j, end=" ")
else:
print(i, end=" ")
print()
1. Layers.i runs from 5 down to 1 — each value is one concentric row.
2. Left half.j runs k..1; print j when j > i, else print i.
3. Right half.j runs 2..k with the same rule so the row mirrors without doubling the center.
Example 2 — User Input (k)
Read k and use a conditional expression for the cell value.
Python
try:
k = int(input("Enter k: "))
except ValueError:
print("Please enter a positive whole number.")
raise SystemExit(1)
if k < 1:
print("Please enter a positive whole number.")
raise SystemExit(1)
for i in range(k, 0, -1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
Output (when user enters 3)
Enter k: 3
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
How It Works
1. Validate k. Catch ValueError; require k >= 1.
2. Same layers. Only the peak changes from the literal 5 to the user value.
3. Safer input tip. Cap demos for readable output:
Safer input tip
if k < 1 or k > 9:
print("Enter a whole number from 1 to 9.")
raise SystemExit(1)
Example 3 — Compact k = 3
Same algorithm with a smaller peak — easier to trace by hand.
Python
k = 3
for i in range(k, 0, -1):
for j in range(k, 0, -1):
print(j if j > i else i, end=" ")
for j in range(2, k + 1):
print(j if j > i else i, end=" ")
print()
Output
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
How It Works
1. Same rules. Only k changes — loops and the j > i rule stay identical.
2. Quick check. Three rows, five columns; center of the last row is 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
j = 1..k
Right half starts at j = 1
The center digit prints twice. Start the mirror loop at j = 2.
i++
Outer loop goes 1..k
You get the inverted order (center first). Keep i from k down to 1.
j < i
Flip the comparison
Using j < i inverts layers. Stay with j if j > i else i.
k = 1
Single cell
Output is just 1 — the right half loop never runs.
print() inside
print() inside a half-loop
That breaks the row. Call bare print() only after both inner loops.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(k²)
O(1)
User input (Example 2)
O(k²)
O(1)
There are k rows and about 2k − 1 cells per row, so total work is quadratic in k.
Remember
Key Takeaways
Rule: for each layer i, print j if j > i else i on both halves.
Mirror: left k..1, right 2..k — skip j = 1 on the right to avoid a double center.
end=" " vs print(): numbers stay on the line; bare print() advances after both halves.
Complexity:O(k²) time; O(1) extra space.
One line: for each layer, print a mirrored row using j if j > i else i.
Frequently Asked Questions
A concentric number square: the outer layer is k (e.g. 5), values decrease toward the center to 1, then mirror back out on each row.
Each row prints a left half (j = k..1) and a right half (j = 2..k) with the same j > i rule, mirroring around the center.
When column j is still outside the current row layer i, print j. Otherwise print i (the current layer value).
The first builds the left descending half; the second mirrors columns 2..k on the right without repeating the center digit.
Change k (or read it from input). Total width becomes 2*k - 1 — see Example 2.
O(k²) because each of k rows prints about 2k - 1 cells.
Program 45 prints a star-and-zero X on a fixed grid. Program 46 prints decreasing/increasing numbers in a concentric square.
Each row has 2*k - 1 numbers. For k = 5, width is 9 columns.
Use try/except ValueError around int(input()) and require k >= 1 — see Example 2.
🤔
Did you know?
Each cell prints j when j > i, else i. Row i runs from k down to 1; grid width = 2k - 1 columns per row.