C++ Program to Print matrix in antispiral form
Last Updated :
20 Feb, 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.
C++
// C++ program to print matrix in anti-spiral form
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 5
void antiSpiralTraversal(int m, int n, int a[R][C])
{
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<int> stk;
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())
{
cout << stk.top() << " ";
stk.pop();
}
}
/* Driver program to test above functions */
int main()
{
int mat[R][C] =
{
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}
};
antiSpiralTraversal(R-1, C-1, mat);
return 0;
}
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 * n) .
Space Complexity: O(m*n) as stack has been created.
Please refer complete article on Print matrix in antispiral form for more details!
Similar Reads
C++ Program to Print matrix in zag-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Approach of C++ code The approach is simple. Just simply iterate over every diagonal elements one at a time and change the directio
3 min read
C++ Program to Print matrix in snake pattern Given an n x n matrix in the given matrix, you have to print the elements of the matrix in the snake pattern. Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {
2 min read
C++ Program To Print Continuous Character Pattern Here we will build a C++ Program To Print Continuous Character patterns using 2 different methods i.e: Using for loopsUsing while loopsInput: rows = 5Output: A B C D E F G H I J K L M N O 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern. The first for loop i
5 min read
C++ Program for Identity Matrix Introduction to Identity Matrix : The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1's and all other elements are zeros. In the below image, every matrix is an Identity Matrix. In linear algebra, this is sometimes call
3 min read
C++ Program to Print the Pattern 'G" In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program,
2 min read