Skip to main content

Multiplication Table

EasyProblem

Description

Given a number n, return its multiplication table: the list n × 1, n × 2, ..., n × 10, in that order.

Examples

Example 1

Input: n = 5

Output: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

Explanation: 5 × 1 is 5, 5 × 2 is 10, and so on up to 5 × 10, 50.

Example 2

Input: n = 1

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Explanation: Each product is just the number it's multiplied by, 1 to 10.

Example 3

Input: n = 12

Output: [12, 24, 36, 48, 60, 72, 84, 96, 108, 120]

Explanation: 12 × 10 is 120, the tenth and last entry.

Constraints

  • 1 ≤ n ≤ 10^8

Editorial

Approach - One Product per Step

Intuition

The table is n × 1, n × 2, and so on up to n × 10: the same multiplication with the counter going from 1 to 10. A counted loop does exactly that.

Mind where the loop ends. Python's range(1, 11) stops before 11, so it gives 1 to 10; in Java, C++ and JavaScript the test i <= 10 keeps 10 in.

Step-by-Step Explanation

Let's trace Example 1, n = 5:

  1. i = 1: 5 × 1 = 5.
  2. i = 2: 5 × 2 = 10.
  3. ... and so on, one product per step ...
  4. i = 10: 5 × 10 = 50, the last one.
  5. Return [5, 10, 15, 20, 25, 30, 35, 40, 45, 50].

One Product per Step — The table fills one slot per step of the loop.

Algorithm

  1. Start with an empty list.
  2. For i from 1 to 10, append n * i.
  3. Return the list.

Code

class Solution {
public:
    vector<int> multiplicationTable(int n) {
        vector<int> table;
        for (int i = 1; i <= 10; i++) table.push_back(n * i);
        return table;
    }
};
from typing import List

class Solution:
    def multiplicationTable(self, n: int) -> List[int]:
        table = []
        for i in range(1, 11):          # 1 to 10
            table.append(n * i)
        return table
class Solution {
    public int[] multiplicationTable(int n) {
        int[] table = new int[10];
        for (int i = 1; i <= 10; i++) table[i - 1] = n * i;
        return table;
    }
}
var multiplicationTable = function(n) {
    const table = [];
    for (let i = 1; i <= 10; i++) table.push(n * i);
    return table;
};

Complexity Analysis

Time Complexity: O(1)

Always ten products, whatever n is.

Space Complexity: O(1)

The ten entries of the answer.

Common Mistakes

Each of these looks right on a quick read, and the tests still reject it. The outputs below come from running the code.

Counting from 0. range(10)

On n = 5 it returns [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]; the answer is [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]. range(10) gives 0 to 9, so the table starts with n × 0 = 0 and stops at n × 9. The table runs from 1 to 10: range(1, 11), since the end isn't included.

Stopping at 9. range(1, 10)

On n = 5 it returns [5, 10, 15, 20, 25, 30, 35, 40, 45]; the answer is [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]. range(1, 10) stops before 10, so the table has nine entries and n × 10 is missing. A range's end is the first number it leaves out.

Multiplying the counter by itself. i * i

On n = 5 it returns [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]; the answer is [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]. i * i gives the squares 1, 4, 9, ..., the same for every n. Each entry is n times the counter: n * i.

Where This Pattern Is Taught

This problem practises a counted loop, and where its range ends. It's taught in Python for DSA, Java for DSA, C++ for DSA and JavaScript for DSA.