You are given a matrix A and and an integer B, you have to perform scalar multiplication of matrix A with an integer B.
Input Format –
First argument is vector of vector of integers A representing matrix.
Second argument is an integer B.
Output format –
You have to return a vector of vector of integers after doing required operations.
Input –
A = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
B = 2
Output –
[[2, 4, 6], [8, 10, 12], [14, 16, 18]]
Explanation –
==> ( [[1, 2, 3],[4, 5, 6],[7, 8, 9]] ) * 2
==> [[2*1, 2*2, 2*3],[2*4, 2*5, 2*6],[2*7, 2*8, 2*9]]
==> [[2, 4, 6], [8, 10, 12], [14, 16, 18]]
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
ArrayList<ArrayList> arrList = new ArrayList<ArrayList>();
ArrayList<ArrayList> finalarr = new ArrayList<ArrayList>();
arrList.add(new ArrayList());
arrList.get(0).add(1);
arrList.get(0).add(2);
arrList.get(0).add(3);
arrList.add(new ArrayList());
arrList.get(1).add(4);
arrList.get(1).add(5);
arrList.get(1).add(6);
arrList.add(new ArrayList());
arrList.get(2).add(7);
arrList.get(2).add(8);
arrList.get(2).add(9);
finalarr = solve(arrList,2);
}
public static ArrayList<ArrayList> solve(ArrayList<ArrayList> A, int B) {
ArrayList<ArrayList> finalarr = new ArrayList<ArrayList>();
for (int i=0; i < A.size(); i++)
{
ArrayList arr = new ArrayList();
for (int j=0; j < A.get(0).size(); j++)
{
arr.add(A.get(i).get(j) * B);
}
finalarr.add(arr);
}
return finalarr;
}
}