
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Write a program in Java to rotate a matrix by 90 degrees in anticlockwise direction
Let’s suppose we have given a square matrix of N×N. The task is to rotate the matrix counterclockwise. For example,
Input-1 −
N = 3 matrix[ ][ ] = [ [1 2 3], [4 5 6], [7 8 9] ]
Output −
3 6 9 2 5 8 1 4 7
Explanation: After rotating the matrix counterclockwise it will generate the output as, 3 6 9 2 5 8 1 4 7.
Approach to solve this problem
Initially the idea is to find the transpose of the given matrix and then swap each of the elements of the matrix while traversing row-wise.
Take Input of a square matrix.
Find the transpose of the matrix.
Swap the element at index 0 with index n-1.
Return the output.
Example
import java.io.*; class Solution { static void rotateMatrix( int n, int matrix[][]){ for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int temp= matrix[i][j]; matrix[i][j]= matrix[j][i]; matrix[j][i]= temp; } } for(int i=0;i<n;i++){ int top=0; int bottom = n-1; while(top<bottom){ int temp = matrix[top][i]; matrix[top][i]=matrix[bottom][i]; matrix[bottom][i] = temp; top++; bottom--; } } } static void displayMatrix(int N, int mat[][]){ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) System.out.print(" " + mat[i][j]); System.out.print("\n"); } System.out.print("\n"); } public static void main(String[] args){ int N = 3; int mat[][] = { {1,2,3}, {4,5,6}, {7,8,9} }; rotateMatrix(N, mat); displayMatrix(N, mat); } }
Output
Running the above code will generate the output as,
3 6 9 2 5 8 1 4 7
- Related Articles
- Program to rotate square matrix by 90 degrees counterclockwise in Python
- Rotate a matrix by 90 degree in clockwise direction without using any extra space in C++
- Rotate a matrix by 90 degree without using any extra space in C++
- How can I rotate xtick labels through 90 degrees in Matplotlib?
- Java Program to Rotate Matrix Elements
- How to rotate a matrix of size n*n to 90 degree using C#?
- Golang Program To Rotate Matrix Elements
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- Rotate the matrix 180 degree in Java?
- Write a program in Python to shift a dataframe index by two periods in positive and negative direction
- Java program to program to cyclically rotate an array by one
- Java program to cyclically rotate an array by one.
- Java Program to Rotate Elements of a List
- Program to rotate a linked list by k places in C++
- Rotate div to -20 degrees angle with CSS

Advertisements