Javascript Program for Rotate the matrix right by K times Last Updated : 23 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4A simple yet effective approach is to consider each row of the matrix as an array and perform an array rotation. This can be done by copying the elements from K to end of array to starting of array using temporary array. And then the remaining elements from start to K-1 to end of the array.Lets take an example: Example: JavaScript // Javascript program to rotate a matrix // right by k times // size of matrix let M = 3; let N = 3; // function to rotate matrix by k times function rotateMatrix(matrix, k) { // temporary array of size M let temp = Array(M).fill(0); // within the size of matrix k = k % M; for (i = 0; i < N; i++) { // copy first M-k elements // to temporary array for (t = 0; t < M - k; t++) temp[t] = matrix[i][t]; // copy the elements from k // to end to starting for (j = M - k; j < M; j++) matrix[i][j - M + k] = matrix[i][j]; // copy elements from // temporary array to end for (j = k; j < M; j++) matrix[i][j] = temp[j - k]; } } // function to display the matrix function displayMatrix(matrix) { for (i = 0; i < N; i++) { let output = ''; for (j = 0; j < M; j++) output += matrix[i][j] + " "; console.log(output); } } // Driver code let matrix = [ [12, 23, 34], [45, 56, 67], [78, 89, 91]]; let k = 2; // rotate matrix by k rotateMatrix(matrix, k); // display rotated matrix displayMatrix(matrix); Output23 34 12 56 67 45 89 91 78 Complexity Analysis:Time Complexity: O(N*M) Auxiliary Space: O(M)Please refer complete article on Rotate the matrix right by K times for more details! Comment More infoAdvertise with us Next Article Javascript Program for Rotate the matrix right by K times K kartik Follow Improve Article Tags : Matrix JavaScript Web Technologies DSA rotation +1 More Practice Tags : Matrix Similar Reads Javascript Program to Print array after it is right rotated K times Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples :Â Â Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: 2 min read Javascript Program for Rotate a Matrix by 180 degree Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9Output : 9 8 7 6 5 4 3 2 1Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1Method: 1 (Only prints rotated 6 min read Javascript Program to Rotate Matrix Elements Given a matrix, clockwise rotate elements in it.Examples:Input: 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12The idea is to use loops similar to the program for printing a matrix in spiral form. 5 min read Javascript Program to Inplace rotate square matrix by 90 degrees | Set 1 Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input:Matrix: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 5 min read Print array after it is right rotated K times | Set 2 Given an array arr[] of size N and a value K, the task is to print the array rotated by K times to the right. Examples: Input: arr = {1, 3, 5, 7, 9}, K = 2Output: 7 9 1 3 5 Input: arr = {1, 2, 3, 4, 5}, K = 4Output: 2 3 4 5 1 Algorithm: The given problem can be solved by reversing subarrays. Below s 13 min read Like