Knowee
Questions
Features
Study Tools

Given a 2D square matrix, rotate the matrix by 90 degrees in clockwise manner.Note: Try to solve it by first scanning the matrix, then do an in-place rotation and then print the rotated matrix.Input FormatFirst line of input contains T - number of test cases. First line of each test case contains N - size of the matrix [NxN]. Its followed by N lines each containing N integers - elements of the matrix.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100Output FormatFor each test case, print the rotated matrix, separated by newline.Sample Input 041121 24 331 2 38 9 47 6 55-44 25 -52 69 -5 17 22 51 27 -44 -79 28 -78 1 -47 65 -77 -14 -21 -6 -96 43 -21 -20 90 Sample Output 0Test Case #1:1 Test Case #2:4 1 3 2 Test Case #3:7 8 1 6 9 2 5 4 3 Test Case #4:-96 65 -79 17 -44 43 -77 28 22 25 -21 -14 -78 51 -52 -20 -21 1 27 69 90 -6 -47 -44 -5

Question

Given a 2D square matrix, rotate the matrix by 90 degrees in clockwise manner.Note: Try to solve it by first scanning the matrix, then do an in-place rotation and then print the rotated matrix.Input FormatFirst line of input contains T - number of test cases. First line of each test case contains N - size of the matrix [NxN]. Its followed by N lines each containing N integers - elements of the matrix.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100Output FormatFor each test case, print the rotated matrix, separated by newline.Sample Input 041121 24 331 2 38 9 47 6 55-44 25 -52 69 -5 17 22 51 27 -44 -79 28 -78 1 -47 65 -77 -14 -21 -6 -96 43 -21 -20 90 Sample Output 0Test Case #1:1 Test Case #2:4 1 3 2 Test Case #3:7 8 1 6 9 2 5 4 3 Test Case #4:-96 65 -79 17 -44 43 -77 28 22 25 -21 -14 -78 51 -52 -20 -21 1 27 69 90 -6 -47 -44 -5

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To rotate a 2D square matrix by 90 degrees in a clockwise manner, we can follow the steps below:

  1. Read the number of test cases, T.
  2. For each test case: a. Read the size of the matrix, N. b. Create a 2D array of size N x N to store the matrix elements. c. Read N lines, each containing N integers, and populate the matrix. d. Perform an in-place rotation of the matrix. e. Print the rotated matrix.

To perform an in-place rotation, we can use the following algorithm:

  1. Iterate through each layer of the matrix, starting from the outermost layer and moving towards the center.
  2. For each layer, iterate through each element in the layer, starting from the leftmost element and moving towards the right.
  3. Swap the values of four elements in a clockwise manner: top-left with bottom-left, bottom-left with bottom-right, bottom-right with top-right, and top-right with top-left.
  4. Continue this process until all layers have been rotated.

Finally, print the rotated matrix for each test case, separated by a newline.

Sample Input: 0 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Sample Output: 0 4 1 5 9 8 2 6 10 12 3 7 11 16 13 14 15

This problem has been solved

Similar Questions

Problem StatementHarish is developing a program for a robotics project that involves processing a 2D grid. The program requires rotating a 2D matrix 90 degrees clockwise in place.He needs to implement the rotateMatrix function to achieve this rotation without using any temporary arrays or data structures, only dynamic memory allocation. Additionally, he needs to provide functions to display the original and rotated matrices. Write a program to accomplish this task.Note: This kind of question will help in clearing HCL recruitment.Input format :The first line of the input consists of an integer 'n', which represents the size of the square matrix (n x n).The second part contains 'n x n' integers representing the elements of the matrix in row-major order.Output format :The first matrix is the original matrix.The second matrix is the matrix after a 90-degree clockwise rotation.Refer to the sample output for formatting specifications.Code constraints :1 <= n <= 100Sample test cases :Input 1 :31 2 34 5 67 8 9Output 1 :Original Matrix:1 2 3 4 5 6 7 8 9 Matrix after 90-degree clockwise rotation:7 4 1 8 5 2 9 6 3 Input 2 :-2Output 2 :Size of the matrix should be positive!Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

Given a 2D square matrix, print the matrix in a spiral order. Refer to examples for more details. From an interview's point of view, after you scan the matrix in a 2D array, try to print the matrix in a spiral order without using any extra space.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains N - the size of the matrix [NxN]. It is followed by N lines each containing N integers - matrix elements.Output FormatFor each test case, print the matrix in a spiral order, separated by newline.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput41121 24 331 2 38 9 47 6 55-44 25 -52 69 -517 22 51 27 -44-79 28 -78 1 -4765 -77 -14 -21 -6-96 43 -21 -20 90Output11 2 3 41 2 3 4 5 6 7 8 9-44 25 -52 69 -5 -44 -47 -6 90 -20 -21 43 -96 65 -79 17 22 51 27 1 -21 -14 -77 28 -78

Rotate Matrix by 90 Degrees

#include <iostream>#include <vector>void rotateMatrix(int** matrix, int n) { // Transpose the matrix for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { std::swap(matrix[i][j], matrix[j][i]); } } // Reverse each row for (int i = 0; i < n; i++) { int left = 0; int right = n - 1; while (left < right) { std::swap(matrix[i][left], matrix[i][right]); left++; right--; } }}void printMatrix(int** matrix, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cout << matrix[i][j] << ' '; } std::cout << std::endl; }}int main() { int n; std::cin >> n; if (n <= 0) { std::cout << "Size of the matrix should be positive!" << std::endl; return 0; } // Allocate dynamic memory for the matrix int** matrix = new int*[n]; for (int i = 0; i < n; i++) { matrix[i] = new int[n]; } // Read the matrix elements for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cin >> matrix[i][j]; } } std::cout << "Original Matrix:" << std::endl; printMatrix(matrix, n); rotateMatrix(matrix, n); std::cout << "Matrix after 90-degree clockwise rotation:" << std::endl; printMatrix(matrix, n); // Deallocate the dynamic memory for (int i = 0; i < n; i++) { delete[] matrix[i]; } delete[] matrix; return 0;}

Given a 2D matrix of size NxN, print the sum of the elements of all its diagonals.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains the N - the size of the matrix. Each of the next N lines contains N integers - the elements of the matrix.Output FormatFor each test case, print the sum of the elements of all the diagonals, separated by a new line. Refer to samples for more clarity.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput43-5 0 42 8 -63 7 11-425 -2-4 16-2 -3 -6 -5 50 38 7 10 -5 -3 306 3 70 9 -20 -7-9 9 -6 7 3 2-1 7 7 6 -4 38 5 6 -9 40 8Output4 -6 4 9 3-4-2 6 -43 80 -15 -29 22 86 51 13 4 4 8ExplanationTest Case 1Sum of the elements of the 1st diagonal: 4Sum of the elements of the 2nd diagonal: 0 + -6 = -6Sum of the elements of the 3rd diagonal: -5 + 8 + 1 = 4Sum of the elements of the 4th diagonal: 2 + 7 = 9Sum of the elements of the 5th diagonal: 3Test Case 2Sum of the elements of the 1st and only diagonal: -4Test Case 3Sum of the elements of the 1st diagonal: -2Sum of the elements of the 2nd diagonal: 5 + 1 = 6Sum of the elements of the 3rd diagonal: -4

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.