A V-shaped alphabet pattern prints letters only on two diagonals that meet at a single tip, with spaces everywhere else so the shape reads as a V in a monospace console.
Remember
Rule: left 0..n when i==j; right n−1..0 when i==k
A A
B B
C C
D D
E ← tip once (end = 'E')
The right scan starts at $n − 1 (not $n) so the tip letter prints once. Compare with Program 33 (inverted V) and Program 32 (solid palindrome pyramid).
Approach
How to Solve It
Two ways to emit the same V — inline diagonal checks, or a shared print_cell helper.
Method
Idea
Best for
Two scans inline
Left 0..$n, right $n−1..0, print when row == col
Learning, interviews, exams
print_cell helper
One function owns the diagonal rule for both legs
Cleaner demos once the tip skip clicks
Pseudocode
Pseudocode
$n = ord($end) - ord('A')
for $i from 0 to $n:
for $j from 0 to $n:
echo ($i == $j ? $alpha[$j] : ' ')
for $k from $n - 1 down to 0:
echo ($i == $k ? $alpha[$k] : ' ')
echo PHP_EOL
1. print_cell owns the rule.$row == $col lives in one place for both legs.
2. Tip still skipped on the right. The right loop starts at $n − 1 even with the helper.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = n
Double tip
If the right scan includes index $n, the last row prints the tip twice. Keep for ($k = $n - 1; $k >= 0; $k--).
No spaces
Collapsed V
Writing only letters (no spaces) packs both legs together. Non-hits must print ' '.
PHP_EOL inside
Column of cells
If echo PHP_EOL is inside either scan, each cell lands on its own line. Use echo for cells; end the row only after both scans.
end = A
Single A
When $n = 0, left prints A and the right loop never runs. A good sanity check.
Wrong compare
Off-diagonal letters
Always compare the row index to the current column ($i == $j / $i == $k), not to a fixed letter.
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
Two scans inline (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet table)
Helper function (Example 3)
O(n²)
O(1)
For letters A..end there are $n + 1 rows and each row scans 2 * $n + 1 cells — quadratic in n.
Remember
Key Takeaways
Rule: print a letter only when row index equals column index.
Single tip: right scan starts at $n − 1, not $n.
Spaces keep the V: non-hits print ' '; break the row with echo PHP_EOL after both scans.
Complexity:O(n²) time; O(1) extra space.
One line: for each row, scan left 0..$n and right $n−1..0, printing a letter only on the diagonal.
Frequently Asked Questions
If the right scan included E, the last row would print E twice (one on each side), breaking the single vertex at the bottom tip of the V.
For n = ord(end) − ord('A'), the left block is n+1 columns and the right block is n columns, so total width is 2n+1 (9 for A..E).
Usually two (one per leg), except the bottom row prints one letter because the right loop cannot match the tip index.
O(n²) because there are n+1 rows and each row scans O(n) positions across both blocks.
Spaces keep column alignment so the two diagonals form a visible V in a monospace console.
echo stays on the same line. echo PHP_EOL ends the current line. Cells use echo; the row break uses echo PHP_EOL after both scans.
Yes. Set $n = ord($end) − ord('A'), scan $j from 0 to $n on the left, and scan $k from $n−1 down to 0 on the right.
Program 20 also practices diagonal alignment with letters; this page focuses on a V made from two opposing diagonal scans that meet at one vertex.
🤔
Did you know?
Outer i is the row index (A..E). Left scan prints only when i == j (main diagonal). Right scan runs from D down to A so the bottom vertex letter (E) appears once.