function solution(arr1, arr2) {
const rows1 = arr1.length
const cols1 = arr1[0].length
const cols2 = arr2[0].length
const result = Array.from({ length: rows1 }, () => Array(cols2).fill(0))
for (let i = 0; i < rows1; i++) {
for (let j = 0; j < cols2; j++) {
for (let k = 0; k < cols1; k++) {
result[i][j] += arr1[i][k] * arr2[k][j]
}
}
}
return result
}
function addMatrices(arr1, arr2) {
return arr1.map((row, i) => row.map((val, j) => val + arr2[i][j]))
}
function transposeMatrix(matrix) {
return matrix[0].map((_, colIndex) => matrix.map((row) => row[colIndex]))
}
function rotateMatrix(matrix) {
return matrix[0].map((_, colIndex) => matrix.map((row) => row[colIndex]).reverse())
}
function scalarMultiply(matrix, scalar) {
return matrix.map((row) => row.map((val) => val * scalar))
}
function columnSum(matrix) {
return matrix[0].map((_, colIndex) => matrix.reduce((sum, row) => sum + row[colIndex], 0))
}
function diagonalSum(matrix) {
const n = matrix.length
let mainDiagonal = 0,
antiDiagonal = 0
for (let i = 0; i < n; i++) {
mainDiagonal += matrix[i][i]
antiDiagonal += matrix[i][n - 1 - i]
}
return { mainDiagonal, antiDiagonal }
}
function findMaxInMatrix(matrix) {
return Math.max(...matrix.flat())
}
function sparseMatrixToList(matrix) {
const result = []
matrix.forEach((row, i) => {
row.forEach((val, j) => {
if (val !== 0) result.push([i, j, val])
})
})
return result
}
function findPath(matrix, start, end) {
const [rows, cols] = [matrix.length, matrix[0].length]
const visited = Array.from({ length: rows }, () => Array(cols).fill(false))
const directions = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
]
function isValid(x, y) {
return x >= 0 && x < rows && y >= 0 && y < cols && !visited[x][y] && matrix[x][y] === 1
}
function dfs(x, y) {
if ([x, y].toString() === end.toString()) return true
visited[x][y] = true
for (const [dx, dy] of directions) {
const [nx, ny] = [x + dx, y + dy]
if (isValid(nx, ny) && dfs(nx, ny)) return true
}
return false
}
return dfs(...start)
}
function divideMatrix(matrix, blockSize) {
const blocks = []
for (let i = 0; i < matrix.length; i += blockSize) {
for (let j = 0; j < matrix[0].length; j += blockSize) {
const block = matrix.slice(i, i + blockSize).map((row) => row.slice(j, j + blockSize))
blocks.push(block)
}
}
return blocks
}
function multiplyMatrices(arr1, arr2) {
const rows1 = arr1.length
const cols1 = arr1[0].length
const cols2 = arr2[0].length
const result = Array.from({ length: rows1 }, () => Array(cols2).fill(0))
for (let i = 0; i < rows1; i++) {
for (let j = 0; j < cols2; j++) {
for (let k = 0; k < cols1; k++) {
result[i][j] += arr1[i][k] * arr2[k][j]
}
}
}
return result
}