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
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].
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.