# Rotate A Matrix Anticlockwise in Java

Programming is always a fun and creative task. The more creative you can think the better programmer you will become. However, programming isn’t always about solving problems sometimes there’s also a fun aspect to it which makes it way more satisfying. If you are a programmer then you might have already played with patterns, strings, etc multiple times. Well, it’s also like that you are already familiar with matrix, but what about Matrix rotation. So in today’s article, we will rotate a matrix anticlockwise in Java. With the help of this program, you will be able to shift a 4*4 matrix in an anticlockwise direction using java language. Practicing these types of questions also helps getting a upper edge in *Competitive Programming.*

So open up your IDE and let’s get started real quick. Once you understand the logic practice this program on your own to make your brain work in a problem-solving way. This is a somewhat hard question you might get asked in an interview so make sure to practice it on your own after reading this article.

## What’s The Approach?

Frankly speaking, there are two approaches which you can follow to solve this question. However one of the approaches is slightly inefficient than the other one. How to make things easy for you we will discuss the most efficient approach for rotating matrix in Java.

As we all know there are **N/2** squares for a Matrix with size **N**. We will execute a loop to traverse the matrix a square at a time, i.e loop from** 0 to N/2 – 1**.

We will rotate four squares at a time so groups in a cycle are **N – 2*i.**

At last, we will print the matrix.

## Java Program To Rotate A Matrix Anticlockwise

// Java program to rotate a // matrix by 90 degrees import java.io.*; class TechDecodeTutorials { // An Inplace function to // rotate a N x N matrix // by 90 degrees in // anti-clockwise direction static void rotateMatrix( int N, int mat[][]) { // Consider all squares one by one for (int x = 0; x < N / 2; x++) { // Consider elements in group // of 4 in current square for (int y = x; y < N - x - 1; y++) { // Store current cell in // temp variable int temp = mat[x][y]; // Move values from right to top mat[x][y] = mat[y][N - 1 - x]; // Move values from bottom to right mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y]; // Move values from left to bottom mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x]; // Assign temp to left mat[N - 1 - y][x] = temp; } } } // Function to print the matrix 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 = 4; int mat[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; rotateMatrix(N, mat); // Print rotated matrix displayMatrix(N, mat); } }

**INPUT: **

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

**OUTPUT : **

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

**Also Read : ****How To Install Visual Studio Code in Windows 11**