All challenges
Easy 0 solved 0% acceptance

Inverted Right Triangle

Print a right triangle upside down: n stars on the first row, one on the last.

What you'll learn

  • Run a loop from n down to 1
  • Relate the row index to the number of symbols
  • Return a pattern as a list of rows

Problem

Flip the right triangle: the first row is the longest, with n stars, and each following row has one star fewer, down to a single *. Counting the outer loop down instead of up is the whole trick.

Implement inverted_right_triangle(n).

Input

n: The number of rows.

Output

Return the rows from top to bottom. Row i (counting from 1) has n − i + 1 * characters.

Constraints

0 ≤ n ≤ 100

Examples

Example 1
Input:
n = 4

Expected output:
expected = ["****", "***", "**", "*"]
Input:
long long n = 4;

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

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

Explanation

Rows shrink by one star: "****", "***", "**", "*".

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

With one row the triangle is just ["*"].

How to approach it

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

Hint 1

Let the outer loop count down from n to 1.

Hint 2

Each row has as many stars as the current loop value.

Hint 3

When n is 0, return an empty list.

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.