An inverted V-shaped hollow pattern prints only the outline of an upside-down V: a single apex on row 1, then two stars that drift farther apart on each later row.
Remember
Rule: star when i == j (left) or i == k (right); else space
*
* *
* *
* *
* * ← 5 rows (width 9)
Unlike the filled inverted pyramid in Program 6, most cells are spaces. This outline is also the upper half of the hollow diamond — flip the outer loop in Program 8 to get the matching upright V.
Approach
How to Solve It
Two ways to emit the same outline — start with if/else legs, then optionally shorten with a ternary.
Method
Idea
Best for
If/else legs
Left $j and right $k loops; star when indices match
Learning, interviews, exams
Ternary ? :
Same bounds; one-line star-vs-space choice
Shorter demos once conditions click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to 1:
print "*" if i == j else " "
for k from 2 to rows:
print "*" if i == k else " "
print newline
1. Same outer loop. Still walk $i from 1 to $rows.
2. Same bounds. Left $j still counts down; right $k still starts at 2.
3. Shorter print.$i == $j ? "*" : " " replaces the multi-line if/else — same decision, less code.
Learn the if/else version first (Examples 1–2) so you can explain the branch in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
$k = 1
Duplicate apex
Starting the right loop at $k = 1 prints two stars on row 1. Keep $k = 2.
$j ascending
Mirrored left leg
The left loop must count $j from $rows down to 1. 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 bare echo for cells; end the row only after both loops.
rows = 1
Single apex
Output is just * — right loop never runs. A good sanity check.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Use is_numeric
Bare (int) casts turn letters into 0 — prefer is_numeric and require $rows >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
If/else legs (Examples 1–2)
O(rows²)
O(1)
Ternary form (Example 3)
O(rows²)
O(1)
About n rows × 2n - 1 characters printed per row — still quadratic in n. Total stars = 2n - 1 (one apex + two per later row).
Remember
Key Takeaways
Rule: print * only when $i == $j (left) or $i == $k (right).
Two legs: left $j counts down; right $k starts at 2.
Break the row: call echo PHP_EOL only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row $i, print a star only when the left or right index matches $i — start the right loop at 2.
Frequently Asked Questions
The outer loop runs i from 1 to rows. For each row, the left loop runs j from rows down to 1 and prints a star only when i equals j. The right loop runs k from 2 to rows and prints a star only when i equals k. Every other cell is a space.
Printing columns from high j to low j places the star for row i when i equals j. As i grows, that match moves leftward in the left block, forming the descending left leg.
On row 1 the left loop already prints the apex at j equals 1. Starting k at 1 would print a second star on that row. Starting at 2 avoids duplicating the tip.
echo stays on the same line. echo PHP_EOL ends the current line. Stars and spaces use echo; the row break uses echo PHP_EOL after both inner loops.
Each line has width 2 * rows - 1: left block length rows, right block length rows - 1.
Program 8 uses the same inner loops but counts the outer loop from rows down to 1, so the wide row prints first and the legs meet at a bottom vertex.
O(n²) for n rows. Each row runs Theta(n) iterations across the two inner loops.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
🤔
Did you know?
This hollow inverted V is the upper half of the hollow diamond. Starting the right loop at $k = 2 is deliberate: on row 1 the left loop already prints the apex, so $k = 1 would duplicate that star.