
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rotate Matrix Elements in Java
In this article, we will understand how to rotate matrix elements using Java. A matrix is a representation of the elements in rows and columns. Matrix rotation is shifting the position of each element of the matrix by 1 position towards its right or left. We will be using two approaches: one where the matrix is rotated based on user input, and another where the matrix is defined within the code.
Problem Statement
Write a Java program to rotate matrix elements. Below is a demonstration of the same ?
Input
The matrix is defined as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output
The matrix after one rotation: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12
Different approaches
Following are the different approaches to rotate matrix elements ?
Using user input
The following are the steps to rotate matrix elements using user-based input ?
- First, we will import the Scanner class from java.util package.
- Take input from the user for matrix dimensions (Rows and Columns).
- Ask the user to input matrix elements row-wise and use m and n to represent rows and columns, and row and column to track current position.
- Rotation Process:
- Left to right along the first row.
- Top to bottom along the last column.
- Right to left across the last row.
- Bottom to top along the first column.
- Display the rotated matrix and print the matrix after one rotation.
Example
In the given example the input is being entered by the user based on a prompt ?
import java.util.Scanner; public class RotateMatrix { static int Rows, Columns; public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows: "); Rows = sc.nextInt(); System.out.print("Enter the number of columns: "); Columns = sc.nextInt(); int input_matrix[][] = new int[Rows][Columns]; System.out.println("Enter the elements of the matrix:"); for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { input_matrix[i][j] = sc.nextInt(); } } System.out.println("The input matrix is defined as:"); for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.print("\n"); } int m = Rows, n = Columns; int row = 0, column = 0; int previous, current; while (row < m && column < n) { if (row + 1 == m || column + 1 == n) break; previous = input_matrix[row + 1][column]; for (int i = column; i < n; i++) { current = input_matrix[row][i]; input_matrix[row][i] = previous; previous = current; } row++; for (int i = row; i < m; i++) { current = input_matrix[i][n-1]; input_matrix[i][n-1] = previous; previous = current; } n--; if (row < m) { for (int i = n-1; i >= column; i--) { current = input_matrix[m-1][i]; input_matrix[m-1][i] = previous; previous = current; } } m--; if (column < n) { for (int i = m-1; i >= row; i--) { current = input_matrix[i][column]; input_matrix[i][column] = previous; previous = current; } } column++; } System.out.println("\nThe matrix after one rotation:"); for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.print("\n"); } sc.close(); } }
Output
The input_matrix is defined as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The input_matrix after one rotation: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12
Using previously defined input
The following are the steps to rotate matrix elements using previously defined input ?
- First, we will define the matrix dimensions (Rows = 4, Columns = 4).
- Define a matrix with predefined values and print the input matrix.
- Call the Rotate_matrix() method, passing the matrix and its dimensions.
- Inside the Rotate_matrix() method: Set initial values for row, column, and previous/current variables.
- Use a while loop to rotate the matrix as long as the matrix is not reduced to a single row or column.
- Rotate the elements in the same order as the first program.
- Update the matrix in place.
- After the loop finishes, print the matrix after rotation.
Example
Here, the integer has been previously defined, and its value is accessed and displayed on the console ?
public class RotateMatrix { static int Rows = 4; static int Columns = 4; static void Rotate_matrix(int m, int n, int matrix[][]) { int row = 0, column = 0; int previous, current; while (row < m && column < n) { if (row + 1 == m || column + 1 == n) break; previous = matrix[row + 1][column]; for (int i = column; i < n; i++) { current = matrix[row][i]; matrix[row][i] = previous; previous = current; } row++; for (int i = row; i < m; i++) { current = matrix[i][n-1]; matrix[i][n-1] = previous; previous = current; } n--; if (row < m) { for (int i = n-1; i >= column; i--) { current = matrix[m-1][i]; matrix[m-1][i] = previous; previous = current; } } m--; if (column < n) { for (int i = m-1; i >= row; i--) { current = matrix[i][column]; matrix[i][column] = previous; previous = current; } } column++; } System.out.println("\nThe matrix after one rotation: "); for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) System.out.print( matrix[i][j] + " "); System.out.print("\n"); } } public static void main(String[] args) { int input_matrix[][] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; System.out.println("The matrix is defined as "); for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) System.out.print( input_matrix[i][j] + " "); System.out.print("\n"); } Rotate_matrix(Rows, Columns, input_matrix); } }
Output
The matrix is defined as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The matrix after one rotation: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12