Given an array arr[] and two values n and m, the task is to fill a matrix of size n*m in a spiral (or circular) fashion (clockwise) with given array elements.
Examples:
Input: n = 4, m = 4, arr[]= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
Output: [[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]]Input: n = 3, m = 4, arr[] = [1, 8, 6, 3, 8, 6, 1, 6, 3, 2, 5, 3]
Output: [[1, 8, 6, 3],
[2, 5, 3, 8],
[3, 6, 1, 6]]
Table of Content
Using Direction Arrays - O(n*m) Time and O(1) Space
The idea is to fill the matrix in a spiral pattern by utilizing a direction array that defines the movement of the traversal. The direction array is a 2D array where each element represents a possible movement in one of four directions: right, down, left, and up.
Please refer to Create Spiral Matrix from Array using Direction Arrays for implementation.
Using Boundary Variables - O(n*m) Time and O(1) Space
The idea is to perform concentric traversal of the matrix, starting from the outermost layer and progressively moving inwards. We begin by filling the top row from left to right, then the rightmost column from top to bottom, followed by the bottom row from right to left, and finally the leftmost column from bottom to top. This process continues until all elements from the input array are placed or the entire matrix is filled.
Step-by-step approach:
- Initialize boundary variables: top = 0, bottom = n - 1, left = 0, and right = m - 1.
- Perform the following steps until all array elements are filled:
- Fill the top row from left to right. Then, increment top.
- Fill the rightmost column from top to bottom. Then, decrement right.
- Fill the bottom row from right to left. Then, decrement bottom.
- Fill the leftmost column from bottom to top. Then, increment left.
Illustration:
// C++ program to create a spiral matrix from given array
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> spiralFill(int n, int m, vector<int> &arr) {
vector<vector<int>> res(n, vector<int>(m, 0));
// boundary variables
int top = 0, bottom = n - 1, left = 0, right = m - 1;
int index = 0;
while (index < arr.size()) {
// Traverse top row from left to right
for (int j = left; j <= right; j++) {
res[top][j] = arr[index++];
}
top++;
// Traverse right most column from top to bottom
for (int i = top; i <= bottom; i++) {
res[i][right] = arr[index++];
}
right--;
// Traverse bottom most row from right to left
if(top <= bottom) {
for (int j = right; j >= left; j--) {
res[bottom][j] = arr[index++];
}
bottom--;
}
// Traverse leftmost column from bottom to up
if(left <= right) {
for (int i = bottom; i >= top; i--) {
res[i][left] = arr[index++];
}
left++;
}
}
return res;
}
int main() {
int m = 4, n = 4;
vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
vector<vector<int>> res = spiralFill(n, m, arr);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << res[i][j] << " ";
}
cout << endl;
}
return 0;
}
// Java program to create a spiral matrix from given array
class GfG {
// Method to fill the matrix in spiral order
static int[][] spiralFill(int n, int m, int[] arr) {
// Result matrix of size n*m
int[][] res = new int[n][m];
// Boundary variables
int top = 0, bottom = n - 1, left = 0, right = m - 1;
int index = 0;
while (index < arr.length) {
// Traverse top row from left to right
for (int j = left; j <= right; j++) {
res[top][j] = arr[index++];
}
top++;
// Traverse rightmost column from top to bottom
for (int i = top; i <= bottom; i++) {
res[i][right] = arr[index++];
}
right--;
// Traverse bottommost row from right to left
if (top <= bottom) {
for (int j = right; j >= left; j--) {
res[bottom][j] = arr[index++];
}
bottom--;
}
// Traverse leftmost column from bottom to up
if (left <= right) {
for (int i = bottom; i >= top; i--) {
res[i][left] = arr[index++];
}
left++;
}
}
return res;
}
public static void main(String[] args) {
int m = 4, n = 4;
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16 };
int[][] res = spiralFill(n, m, arr);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(res[i][j] + " ");
}
System.out.println();
}
}
}
# Python program to create a spiral matrix from given array
def spiralFill(n, m, arr):
res = [[0 for _ in range(m)] for _ in range(n)]
# boundary variables
top, bottom, left, right = 0, n - 1, 0, m - 1
index = 0
while index < len(arr):
# Traverse top row from left to right
for j in range(left, right + 1):
res[top][j] = arr[index]
index += 1
top += 1
# Traverse right most column from top to bottom
for i in range(top, bottom + 1):
res[i][right] = arr[index]
index += 1
right -= 1
# Traverse bottom most row from right to left
if top <= bottom:
for j in range(right, left - 1, -1):
res[bottom][j] = arr[index]
index += 1
bottom -= 1
# Traverse leftmost column from bottom to up
if left <= right:
for i in range(bottom, top - 1, -1):
res[i][left] = arr[index]
index += 1
left += 1
return res
if __name__ == "__main__":
m, n = 4, 4
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
res = spiralFill(n, m, arr)
for row in res:
print(" ".join(map(str, row)))
//C# program to create a spiral matrix from given array
using System;
class GfG {
// Method to fill the matrix in spiral order
static int[, ] SpiralFill(int n, int m, int[] arr) {
// Result matrix of size n*m
int[, ] res = new int[n, m];
// Boundary variables
int top = 0, bottom = n - 1, left = 0,
right = m - 1;
int index = 0;
while (index < arr.Length) {
// Traverse top row from left to right
for (int j = left; j <= right; j++) {
res[top, j] = arr[index++];
}
top++;
// Traverse rightmost column from top to bottom
for (int i = top; i <= bottom; i++) {
res[i, right] = arr[index++];
}
right--;
// Traverse bottommost row from right to left
if (top <= bottom) {
for (int j = right; j >= left; j--) {
res[bottom, j] = arr[index++];
}
bottom--;
}
// Traverse leftmost column from bottom to up
if (left <= right) {
for (int i = bottom; i >= top; i--) {
res[i, left] = arr[index++];
}
left++;
}
}
return res;
}
static void Main(string[] args) {
int m = 4, n = 4;
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16 };
int[, ] res = SpiralFill(n, m, arr);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Console.Write(res[i, j] + " ");
}
Console.WriteLine();
}
}
}
// JavaScript program to create a spiral
// matrix from given array
function spiralFill(n, m, arr) {
// Result matrix of size n*m
let res = Array.from({length: n}, () => Array(m).fill(0));
// boundary variables
let top = 0, bottom = n - 1, left = 0, right = m - 1;
let index = 0;
while (index < arr.length) {
// Traverse top row from left to right
for (let j = left; j <= right; j++) {
res[top][j] = arr[index++];
}
top++;
// Traverse rightmost column from top to bottom
for (let i = top; i <= bottom; i++) {
res[i][right] = arr[index++];
}
right--;
// Traverse bottommost row from right to left
if(top <= bottom) {
for (let j = right; j >= left; j--) {
res[bottom][j] = arr[index++];
}
bottom--;
}
// Traverse leftmost column from bottom to up
if(left <= right) {
for (let i = bottom; i >= top; i--) {
res[i][left] = arr[index++];
}
left++;
}
}
return res;
}
// Driver Code
let m = 4, n = 4;
let arr = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
];
let res = spiralFill(n, m, arr);
res.forEach(row => console.log(row.join(" ")));
Output
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
Related articles: