All challenges
Medium 0 solved 0% acceptance

Double Staircase

Print two staircases of # blocks that face each other across a two-space gap.

What you'll learn

  • Right-align one part of a row and left-align another
  • Assemble a row from several pieces
  • Avoid trailing spaces in output rows

Problem

Two staircases of height n stand back to back with a gap between them, like the facing pyramids in an old platform game. Row i (counting from 1) has n − i leading spaces, then i # characters, then exactly two spaces, then i more # characters. The left staircase is aligned to the right edge and the right one to the left edge.

Implement double_staircase(n).

Input

n: The height of each staircase.

Output

Return the rows from top to bottom. Every row ends with a #, so there are no trailing spaces.

Constraints

0 ≤ n ≤ 50

Examples

Example 1
Input:
n = 3

Expected output:
expected = ["  #  #", " ##  ##", "###  ###"]
Input:
long long n = 3;

Expected output:
std::vector<std::string> expected = {"  #  #", " ##  ##", "###  ###"};
Input:
long n = 3;

Expected output:
string[] expected = new string[] {"  #  #", " ##  ##", "###  ###"};

Explanation

Row 1 is 2 spaces, #, 2 spaces, #; row 3 has no leading spaces: [" # #", " ## ##", "### ###"].

Example 2
Input:
n = 1

Expected output:
expected = ["#  #"]
Input:
long long n = 1;

Expected output:
std::vector<std::string> expected = {"#  #"};
Input:
long n = 1;

Expected output:
string[] expected = new string[] {"#  #"};

Explanation

One block on each side of the gap: ["# #"].

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Build the left staircase first: it is a right-aligned triangle.

Hint 2

The gap is always exactly two spaces.

Hint 3

The right staircase has the same width as the left one on the same row and needs no padding after it.

Expected complexity

O(n^2) time, O(n^2) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

The best passing solution of each coder. Faster, smaller and better-reviewed solutions rank higher.

No passing solutions yet. Solve it to take first place.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.