Back to course

Matrix Scalar Product

Content Reader1 words • 0:00 • Browser TTS

image.png

Matrix Scalar Product

Problem Statement:

Given matrix A and integer B, return new matrix where each element is multiplied by B.

Examples:

Example 1:

Input: A = [[1,2],3,4], B = 2

Output: [[2,4],6,8]

Approach:

Iterate through all elements, multiply by scalar.

Time Complexity:

  • Time = O(N×M), Space = O(N×M)

JavaScript Code:

function scalarMultiply(A, B) {
    return A.map(row => row.map(val => val * B));
}

Key Takeaways:

  1. Basic matrix operation.
  2. Apply scalar to each element.
  3. Can be done in-place or new matrix.
  4. Foundation for matrix transformations.
  5. Simple but essential operation.
Photo of Rahul Aher

Written by Rahul Aher

I'm Rahul, Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.

More about me