Open In App

Java Program to Rotate matrix by 45 degrees

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a matrix mat[][] of size N*N, the task is to rotate the matrix by 45 degrees and print the matrix.

Examples:

Input: N = 6, 
mat[][] = {{3, 4, 5, 1, 5, 9, 5}, 
               {6, 9, 8, 7, 2, 5, 2},  
               {1, 5, 9, 7, 5, 3, 2}, 
               {4, 7, 8, 9, 3, 5, 2}, 
               {4, 5, 2, 9, 5, 6, 2}, 
               {4, 5, 7, 2, 9, 8, 3}}
Output:
        3
      6 4
    1 9 5
   4 5 8 1
  4 7 9 7 5
4 5 8 7 2 9
  5 2 9 5 5
   7 9 3 3
    2 5 5
     9 6
      8

Input: N = 4, 
mat[][] = {{2, 5, 7, 2}, 
                {9, 1, 4, 3}, 
                {5, 8, 2, 3}, 
                {6, 4, 6, 3}}

Output:
    2
  9 5
 5 1 7
6 8 4 2
 4 2 3
  6 3
   3 

Approach: Follow the steps given below in order to solve the problem:

  1. Store the diagonal elements in a list using a counter variable.
  2. Print the number of spaces required to make the output look like the desired pattern.
  3. Print the list elements after reversing the list.
  4. Traverse through only diagonal elements to optimize the time taken by the operation.

Below is the implementation of the above approach:

Java




// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to rotate
// matrix by 45 degree
static void matrix(int n, int m,
                   int [][]li)
{
  // Counter Variable
  int ctr = 0;
 
  while (ctr < 2 * n - 1)
  {
    for(int i = 0; i < Math.abs(n - ctr - 1);
            i++)
    {
      System.out.print(" ");
    }
 
    Vector<Integer> lst = new Vector<Integer>();
 
    // Iterate [0, m]
    for(int i = 0; i < m; i++)
    {
      // Iterate [0, n]
      for(int j = 0; j < n; j++)
      {
        // Diagonal Elements
        // Condition
        if (i + j == ctr)
        {
          // Appending the
          // Diagonal Elements
          lst.add(li[i][j]);
        }
      }
    }
 
    // Printing reversed Diagonal
    // Elements
    for(int i = lst.size() - 1; i >= 0; i--)
    {
      System.out.print(lst.get(i) + " ");
    }
     
    System.out.println();
    ctr += 1;
  }
}
 
// Driver code   
public static void main(String[] args)
{   
  // Dimensions of Matrix
  int n = 8;
  int m = n;
 
  // Given matrix
  int[][] li = {{4, 5, 6, 9, 8, 7, 1, 4},
                {1, 5, 9, 7, 5, 3, 1, 6},
                {7, 5, 3, 1, 5, 9, 8, 0},
                {6, 5, 4, 7, 8, 9, 3, 7},
                {3, 5, 6, 4, 8, 9, 2, 1},
                {3, 1, 6, 4, 7, 9, 5, 0},
                {8, 0, 7, 2, 3, 1, 0, 8},
                {7, 5, 3, 1, 5, 9, 8, 5}};
 
  // Function call
  matrix(n, m, li);
}
}
 
// This code is contributed by Princi Singh


Output

       4 
      1 5 
     7 5 6 
    6 5 9 9 
   3 5 3 7 8 
  3 5 4 1 5 7 
 8 1 6 7 5 3 1 
7 0 6 4 8 9 1 4 
 5 7 4 8 9 8 6 
  3 2 7 9 3 0 
   1 3 9 2 7 
    5 1 5 1 
     9 0 0 
      8 8 
       5 

Time Complexity: O(N2)
Auxiliary Space: O(1)

Approach: Rotating Matrix by 45 Degrees using a new matrix

The steps of this approach are:

  1. Create a new matrix with dimensions (2N-1)x(2N-1), where N is the size of the input matrix. This new matrix will store the rotated matrix.
  2. Iterate through each element of the input matrix and place it in the correct position in the rotated matrix according to the rotation formula.
  3. Iterate through each element of the rotated matrix and print it in the correct order, with empty spaces for the elements that were not filled by the input matrix.

Java




public class Main {
    public static void main(String[] args) {
        int N = 4;
        int[][] mat = {{2, 5, 7, 2}, {9, 1, 4, 3}, {5, 8, 2, 3}, {6, 4, 6, 3}};
 
        int[][] result = new int[2*N-1][2*N-1];
 
        // Rotate the matrix by 45 degrees
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                result[i+j][N-1-i+j] = mat[i][j];
            }
        }
 
        // Print the rotated matrix
        for (int i = 0; i < 2*N-1; i++) {
            for (int j = 0; j < 2*N-1; j++) {
                if (result[i][j] != 0) {
                    System.out.print(result[i][j] + " ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}


Output

      2       
    9   5     
  5   1   7   
6   8   4   2 
  4   2   3   
    6   3     
      3       

The time complexity: O(N^2) and

The Auxiliary space is also O(N^2)

Please refer complete article on Rotate matrix by 45 degrees for more details!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads