We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/**
* Complete the matrixRotation function below.
* @param {number[][]} matrix - 2D array of integers
* @param {number} r - rotation factor
*/
function matrixRotation(matrix, r) {
const m = matrix.length; // Number of rows
const n = matrix[0].length; // Number of columns
// If matrix is empty or has only one element, no rotation needed
if (m === 0 || n === 0 || (m === 1 && n === 1)) {
printMatrix(matrix);
return;
}
// Calculate the number of "rings" in the matrix
const numRings = Math.min(m, n) / 2;
// For each ring
for (let ring = 0; ring < numRings; ring++) {
// Calculate the dimensions of current ring
const rowStart = ring;
const rowEnd = m - 1 - ring;
const colStart = ring;
const colEnd = n - 1 - ring;
// Calculate the number of elements in this ring
const ringSize = 2 * (rowEnd - rowStart + colEnd - colStart);
// Extract the ring elements into a 1D array
const ringElements = [];
// Top row (left to right)
for (let j = colStart; j < colEnd; j++) {
ringElements.push(matrix[rowStart][j]);
}
// Right column (top to bottom)
for (let i = rowStart; i < rowEnd; i++) {
ringElements.push(matrix[i][colEnd]);
}
// Bottom row (right to left)
for (let j = colEnd; j > colStart; j--) {
ringElements.push(matrix[rowEnd][j]);
}
// Left column (bottom to top)
for (let i = rowEnd; i > rowStart; i--) {
ringElements.push(matrix[i][colStart]);
}
// Calculate the effective number of rotations
// For anti-clockwise rotation, we need to rotate in the opposite direction
const effectiveRotations = r % ringElements.length;
// Rotate the ring by effectiveRotations (anti-clockwise)
const rotatedRing = [];
for (let i = 0; i < ringElements.length; i++) {
const newIndex = (i - effectiveRotations + ringElements.length) % ringElements.length;
rotatedRing[i] = ringElements[newIndex];
}
// Put the rotated elements back into the matrix
let idx = 0;
// Top row (left to right)
for (let j = colStart; j < colEnd; j++) {
matrix[rowStart][j] = rotatedRing[idx++];
}
// Right column (top to bottom)
for (let i = rowStart; i < rowEnd; i++) {
matrix[i][colEnd] = rotatedRing[idx++];
}
// Bottom row (right to left)
for (let j = colEnd; j > colStart; j--) {
matrix[rowEnd][j] = rotatedRing[idx++];
}
// Left column (bottom to top)
for (let i = rowEnd; i > rowStart; i--) {
matrix[i][colStart] = rotatedRing[idx++];
}
}
// Print the rotated matrix
printMatrix(matrix);
}
/**
* Helper function to print the matrix
* @param {number[][]} matrix - 2D array of integers
*/
function printMatrix(matrix) {
for (let i = 0; i < matrix.length; i++) {
console.log(matrix[i].join(' '));
}
}
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
Here is the solution in Javascript
/** * Complete the matrixRotation function below. * @param {number[][]} matrix - 2D array of integers * @param {number} r - rotation factor */ function matrixRotation(matrix, r) { const m = matrix.length; // Number of rows const n = matrix[0].length; // Number of columns
}
/** * Helper function to print the matrix * @param {number[][]} matrix - 2D array of integers */ function printMatrix(matrix) { for (let i = 0; i < matrix.length; i++) { console.log(matrix[i].join(' ')); } }
// Example usage: function main() { // Example 1 const matrix1 = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ]; console.log("Example 1 output:"); matrixRotation(matrix1, 2);
}
main();
I even use this solution on my website :: https://goldprice.tr/سعر-الذهب-مباشر/