An inverted V-shaped alphabet pattern prints a single tip letter at the top, then matching letter pairs that drift farther apart on each lower row.
Remember
Rule: left print when $i == $j; right print when $i == $k
(right scan starts at B so tip A stays alone)
A
B B
C C
D D
E E ← A–E (width 9)
Geometry matches the hollow inverted V in Star Pattern 7, but cells print letters instead of *. Stack a mirrored lower half in Program 34 to close a full alphabet diamond.
Approach
How to Solve It
Two ways to emit the same outline — start with if/else legs, then optionally share a cell helper.
Method
Idea
Best for
If/else legs
Left $j and right $k scans; letter when indices match
Learning, interviews, exams
Helper + ternary
One print_cell($row, $col) used by both legs
Less duplication once the diagonals click
Pseudocode
Pseudocode
$n = ord($end) - ord('A') // 4 when end is 'E'
for $i from 0 to $n:
for $j from $n down to 0:
echo $alpha[$j] if $i == $j else " "
for $k from 1 to $n:
echo $alpha[$k] if $i == $k else " "
echo PHP_EOL
1. One cell rule.print_cell owns the $row == $col decision and the space fallback.
2. Same bounds. Left still counts down from $n; right still starts at 1.
3. Same shape, less copy-paste. Learn the expanded if/else first (Example 1), then refactor when the diagonals feel familiar.
Edge Cases & Pitfalls
Check these before calling the solution done.
$k = 0
Duplicate tip A
Starting the right loop at $k = 0 prints two As on the first row. Keep $k = 1.
$j ascending
Mirrored left leg
The left loop must count $j from $n down to 0. Ascending $j flips the left diagonal.
PHP_EOL inside
Broken outline
If echo PHP_EOL is inside either inner loop, each cell lands on its own line. Use echo for cells; end the row only after both loops.
end = A
Single tip
Output is just A — right loop never runs. A good sanity check.
Proportional font
Looks skewed in the IDE
Spaces and letters need a monospace font. Proportional fonts make diagonals look uneven.
Bad input
Validate one letter
Empty lines and multi-character input break naive $line[0] indexing — require a single A–Z character.
Analysis
Time and Space Complexity
Program
Time
Extra space
If/else legs (Examples 1–2)
O(n²)
O(1) beyond the alphabet string
Helper function (Example 3)
O(n²)
O(1) beyond the alphabet string
About n + 1 rows × 2n + 1 characters printed per row — still quadratic in n. Total letters = 2n + 1 (one tip + two per later row).
Remember
Key Takeaways
Rule: print $alpha[$col] only when $row == $col; otherwise a space.
Two legs: left $j counts down from $n; right $k starts at 1.
Break the row: call echo PHP_EOL only after both inner loops.
Complexity:O(n²) time; O(1) extra space beyond the alphabet string.
One line: for each row $i, print a letter only when the left or right column index matches $i — start the right loop at 1.
Frequently Asked Questions
Because the right block starts from index 1 (letter B), so it never matches i equals 0. Only the left block prints A on the first row.
Width is 2n+1: n+1 columns from the left block and n columns from the right block. For A–E, n is 4 and width is 9.
Program 31 is wide at the top and has a single bottom vertex. Program 33 has a single A at the top and widens downward with pairs like B B, C C.
echo stays on the same line. echo PHP_EOL ends the current line. Letters and spaces use echo; the row break uses echo PHP_EOL after both inner loops.
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 open into a visible inverted V in a monospace console.
Use trim(fgets(STDIN)), require a single A–Z character after strtoupper, and reject empty or multi-character input.
Program 34 reuses this inverted-V row logic for A..E, then mirrors D..A downward to close a full diamond without repeating the widest E row.
🤔
Did you know?
This inverted V is the upper half of the alphabet diamond. Starting the right loop at $k = 1 (letter B) is deliberate: on row 0 the left loop already prints the tip A, so visiting index 0 again would duplicate it.