Java Program to Print matrix in antispiral form
Last Updated :
27 Jan, 2023
Given a 2D array, the task is to print matrix in anti spiral form:
Examples:

Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Input : arr[][4] = {1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
13, 14, 15, 16};
Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1
Input :arr[][6] = {1, 2, 3, 4, 5, 6
7, 8, 9, 10, 11, 12
13, 14, 15, 16, 17, 18};
Output : 11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1
The idea is simple, we traverse matrix in spiral form and put all traversed elements in a stack. Finally one by one elements from stack and print them.
Java
// Java Code for Print matrix in antispiral form
import java.util.*;
class GFG {
public static void antiSpiralTraversal(int m, int n,
int a[][])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator */
Stack<Integer> stk=new Stack<Integer>();
while (k <= m && l <= n)
{
/* Print the first row from the remaining
rows */
for (i = l; i <= n; ++i)
stk.push(a[k][i]);
k++;
/* Print the last column from the remaining
columns */
for (i = k; i <= m; ++i)
stk.push(a[i][n]);
n--;
/* Print the last row from the remaining
rows */
if ( k <= m)
{
for (i = n; i >= l; --i)
stk.push(a[m][i]);
m--;
}
/* Print the first column from the remaining
columns */
if (l <= n)
{
for (i = m; i >= k; --i)
stk.push(a[i][l]);
l++;
}
}
while (!stk.empty())
{
System.out.print(stk.peek() + " ");
stk.pop();
}
}
/* Driver program to test above function */
public static void main(String[] args)
{
int mat[][] =
{
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}
};
antiSpiralTraversal(mat.length - 1, mat[0].length - 1,
mat);
}
}
// This code is contributed by Arnav Kr. Mandal.
Output:
12 13 14 9 8 7 6 11 16 17 18 19 20 15 10 5 4 3 2 1
Time Complexity: O(m x n) where m is the number of rows and n is the number of columns in the matrix.
Space Complexity: O(m x n) as we are using a stack to store the elements.
Please refer complete article on Print matrix in antispiral form for more details!
Similar Reads
Java Program to Print Matrix in Z form Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9 Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 Java // Java program to print a
2 min read
How to read a Matrix from user in Java? Given task is to read a matrix from the user. The size and number of elements of matrices are to be read from the keyboard. Java // Java program to read a matrix from user import java.util.Scanner; public class MatrixFromUser { // Function to read matrix public static void readMatrixByUser() { int m
2 min read
Simplest and Best method to print a 2D Array in Java Given a 2d array arr in Java, the task is to print the contents of this 2d array. Method 1: Loop method The first thing that comes to mind is to write a nested for loop, and print each element by arr[i][j]. Java // Java program to print 2d array // using Loop method import java.io.*; import java.uti
2 min read
Java Arrays Coding Practice Problems Arrays are a fundamental data structure in Java programming, enabling efficient storage, manipulation, and retrieval of elements. This collection of Java array practice problems covers essential operations, including array traversal, sorting, searching, matrix manipulations, and element-wise calcula
2 min read
Sorting a 2D Array according to values in any given column in Java We are given a 2D array of order N X M and a column number K ( 1<=K<=m). Our task is to sort the 2D array according to values in Column K.Examples:Input: If our 2D array is given as (Order 4X4) 39 27 11 42 10 93 91 90 54 78 56 89 24 64 20 65Sorting it by values in column 3 Output: 39 27 11 422
3 min read
Java Array Exercise An array is a group of elements with similar data types that are bound together as one. The array allows us to store and manipulate a collection of elements together. Mastering arrays is essential for any Java developer, as it provides a solid foundation for more complex data structures and algorith
7 min read