Table of contents
1.
Introduction
2.
Problem Statement
3.
Program Code in C++
3.1.
Complexity Analysis
4.
Frequently Asked Questions
4.1.
Is it necessary that the input matrix should be a two-dimensional one?
4.2.
Where will the spiral form start from?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Matrix in Reverse Spiral Form

Author Ayushi Poddar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

The following article discusses one such puzzle so let's get right to it.

Problem Statement

A spiral matrix problem always takes a 2-Dimensional array of, say, n-rows and m-columns as input to the program and then prints all the array elements in reverse of the spiral order.

Example :

If this is the input matrix -

Illustration Matrix

The Spiral Matrix would be formed like this -

Illustration Matrix

Thus the output of the program, which is the reverse spiral form, will be as follows-

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

Recommended: Try the Problem yourself before moving on to the solution.

Explanation

This spiral order begins at the very top left corner of the input matrix and then moves on to the element it encounters while forming a loop around the input matrix, towards the center of this particular matrix, and always in a clockwise manner. Finally, the reverse of this order is given as output to the problem statement of printing an array in the reverse spiral order.

Program Code in C++

#include <iostream>
using namespace std;

void ReverseSpiral(int m, int n, int a[r][c])
{
long int input[100];
int i, x = 0, y= 0; // x - starting row index and y- starting column index
int z = 0; // Counter for the 1D array that will store the spiral form 
int size = m*n; //Total number of elements
while (x < m && y < n)
{
	int val;
	for (i = y; i < n; ++i)
	{
	// printf("%d ", a[x][i]);
		val = a[x][i];
		input[z] = val;
		++z;
	}
x++;


/* Print the last column from the remaining columns */
for (i = x; i < m; ++i)
{
	// printf("%d ", a[i][n-1]);
	val = a[i][n-1];
	input[z] = val;
	++z;
}
n--;


if ( x < m)
{
	for (i = n-1; i >= y; --i)
	{
		printf("%d ", a[m-1][i]);
		val = a[m-1][i];
		input[z] = val;
		++z;
	}
m--;
}


if (y < n)
{
	for (i = m-1; i >= x; --i)
	{
		// printf("%d ", a[i][x]);
		val = a[i][l];
		b[z] = val;
		++z;
	}
y++;
}
}
for (int i=size-1 ; i>=0 ; --i)
{
	cout<<input[i]<<" ";
}
}
int main()
{
int a[r][c] = { {1, 2, 3, 4},{12,13,14,5},{11,16,15,6},{10,9,8,7}};
ReverseSpiral(r, c, a);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output 

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

Complexity Analysis

Time Complexity: O(n) as the for loop runs for n number of times.

Space Complexity: O(n) as the array int a is being taken which occupies space in the memory.

Check out this problem - Reverse Nodes In K Group
Must Read  C Program to Reverse a Number

Frequently Asked Questions

Is it necessary that the input matrix should be a two-dimensional one?

Yes, it is necessary that the input matrix to find out the reverse spiral matrix should be a 2D matrix only because otherwise, we won’t be able to form a spiral in the case of a 1D matrix.

Where will the spiral form start from?

For finding the spiral form of the input matrix, we will always be starting with the topmost left corner element, and then we move clockwise.

Conclusion

This article extensively discussed the problem of printing the reverse spiral form of a matrix. Spiral form, as the name suggests, is formed by forming a spiral around a matrix starting from the top leftmost element and moving to the center of the matrix. 

Recommended Readings:


Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Live masterclass