All challenges
Easy 0 solved 0% acceptance

Multiplication Grid

Build a full multiplication table.

What you'll learn

  • Build a table with two loops
  • Create a list of lists
  • Index rows and columns

Problem

Build an n × n multiplication table: the value in row i, column j (both counting from 1) is i × j.

Implement multiplication_grid(n).

Input

n: The table size.

Output

Return the table as a list of rows.

Constraints

0 ≤ n ≤ 20

Examples

Example 1
Input:
n = 3

Expected output:
expected = [
    [1, 2, 3],
    [2, 4, 6],
    [3, 6, 9]
]
Input:
long long n = 3;

Expected output:
std::vector<std::vector<long long>> expected = {
    {1, 2, 3},
    {2, 4, 6},
    {3, 6, 9}
};
Input:
long n = 3;

Expected output:
long[][] expected = new long[][] {
    new long[] {1, 2, 3},
    new long[] {2, 4, 6},
    new long[] {3, 6, 9}
};

Explanation

Rows are [1, 2, 3], [2, 4, 6] and [3, 6, 9].

Example 2
Input:
n = 1

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

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

Expected output:
long[][] expected = new long[][] {
    new long[] {1}
};

Explanation

A 1×1 table holds only 1 × 1 = 1.

How to approach it

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

Hint 1

Every cell is just the product of its row number and column number.

Hint 2

Construct the table row by row, filling each row left to right.

Hint 3

Handle the zero-size case by returning an empty table, not a single empty row.

Expected complexity

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

References

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

Learn from others

Community solutions

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