Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)

Given two values m and n, fill a matrix of size ‘m*n’ in spiral (or circular) fashion (clockwise) with natural numbers from 1 to m*n.

Examples:

Input : m = 4, n = 4
Output :  1  2  3  4
         12 13 14  5
         11 16 15  6
         10  9  8  7 

Input : m = 3, n = 4
Output :  1  2  3  4
          10 11 12 5
          9  8  7  6     



The idea is based on Print a given matrix in spiral form. We create a matrix of size m * n and traverse it in spiral fashion. While traversing, we keep track of a variable “val” to fill next value, we increment “val” one by one and put its values in the matrix.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to fill a matrix with values from
// 1 to n*n in spiral fashion.
#include <bits/stdc++.h>
using namespace std;
  
const int MAX = 100;
  
// Fills a[m][n] with values from 1 to m*n in
// spiral fashion.
void spiralFill(int m, int n, int a[][MAX])
{
    // Initialize value to be filled in matrix
    int val = 1;
  
    /*  k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index */
    int k = 0, l = 0;
    while (k < m && l < n)
    {
        /* Print the first row from the remaining
          rows */
        for (int i = l; i < n; ++i)
            a[k][i] = val++;
  
        k++;
  
        /* Print the last column from the remaining
          columns */
        for (int i = k; i < m; ++i)
            a[i][n-1] = val++;
        n--;
  
        /* Print the last row from the remaining
           rows */
        if (k < m)
        {
            for (int i = n-1; i >= l; --i)
                a[m-1][i] = val++;
            m--;
        }
  
        /* Print the first column from the remaining
           columns */
        if (l < n)
        {
            for (int i = m-1; i >= k; --i)
                 a[i][l] = val++;
            l++;
        }
    }
}
  
/* Driver program to test above functions */
int main()
{
    int m = 4, n = 4;
    int a[MAX][MAX];
    spiralFill(m, n, a);
    for (int i=0; i<m; i++)
    {
       for (int j=0; j<n; j++)
          cout << a[i][j] << " ";
       cout << endl;
    }
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to fill a matrix with values from
// 1 to n*n in spiral fashion.
class GFG {
  
    static int MAX = 100;
  
// Fills a[m][n] with values from 1 to m*n in
// spiral fashion.
    static void spiralFill(int m, int n, int a[][]) {
        // Initialize value to be filled in matrix
        int val = 1;
  
        /*  k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index */
        int k = 0, l = 0;
        while (k < m && l < n) {
            /* Print the first row from the remaining
          rows */
            for (int i = l; i < n; ++i) {
                a[k][i] = val++;
            }
  
            k++;
  
            /* Print the last column from the remaining
          columns */
            for (int i = k; i < m; ++i) {
                a[i][n - 1] = val++;
            }
            n--;
  
            /* Print the last row from the remaining
           rows */
            if (k < m) {
                for (int i = n - 1; i >= l; --i) {
                    a[m - 1][i] = val++;
                }
                m--;
            }
  
            /* Print the first column from the remaining
           columns */
            if (l < n) {
                for (int i = m - 1; i >= k; --i) {
                    a[i][l] = val++;
                }
                l++;
            }
        }
    }
  
    /* Driver program to test above functions */
    public static void main(String[] args) {
        int m = 4, n = 4;
        int a[][] = new int[MAX][MAX];
        spiralFill(m, n, a);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println("");
        }
    }
}
  
/* This Java code is contributed by PrinciRaj1992*/

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to fill a matrix with values from
// 1 to n*n in spiral fashion.
  
using System;
class GFG {
   
    static int MAX = 100;
   
// Fills a[m,n] with values from 1 to m*n in
// spiral fashion.
    static void spiralFill(int m, int n, int[,] a) {
        // Initialize value to be filled in matrix
        int val = 1;
   
        /*  k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index */
        int k = 0, l = 0;
        while (k < m && l < n) {
            /* Print the first row from the remaining
          rows */
            for (int i = l; i < n; ++i) {
                a[k,i] = val++;
            }
   
            k++;
   
            /* Print the last column from the remaining
          columns */
            for (int i = k; i < m; ++i) {
                a[i,n - 1] = val++;
            }
            n--;
   
            /* Print the last row from the remaining
           rows */
            if (k < m) {
                for (int i = n - 1; i >= l; --i) {
                    a[m - 1,i] = val++;
                }
                m--;
            }
   
            /* Print the first column from the remaining
           columns */
            if (l < n) {
                for (int i = m - 1; i >= k; --i) {
                    a[i,l] = val++;
                }
                l++;
            }
        }
    }
   
    /* Driver program to test above functions */
    public static void Main() {
        int m = 4, n = 4;
        int[,] a = new int[MAX,MAX];
        spiralFill(m, n, a);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                Console.Write(a[i,j] + " ");
            }
            Console.Write("\n");
        }
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to fill a matrix with values 
// from 1 to n*n in spiral fashion.
  
// Fills a[m][n] with values from 1 to 
// m*n in spiral fashion.
function spiralFill($m, $n, &$a)
{
    // Initialize value to be filled 
    // in matrix
    $val = 1;
  
    /* k - starting row index
       m - ending row index
       l - starting column index
       n - ending column index */
    $k = 0;
    $l = 0;
    while ($k < $m && $l < $n)
    {
        /* Print the first row from
        the remaining rows */
        for ($i = $l; $i < $n; ++$i)
            $a[$k][$i] = $val++;
  
        $k++;
  
        /* Print the last column from
        the remaining columns */
        for ($i = $k; $i < $m; ++$i)
            $a[$i][$n - 1] = $val++;
        $n--;
  
        /* Print the last row from 
        the remaining rows */
        if ($k < $m)
        {
            for ($i = $n - 1; $i >= $l; --$i)
                $a[$m - 1][$i] = $val++;
            $m--;
        }
  
        /* Print the first column from
           the remaining columns */
        if ($l < $n)
        {
            for ($i = $m - 1; $i >= $k; --$i)
                $a[$i][$l] = $val++;
            $l++;
        }
    }
}
  
// Driver Code
$m = 4;
$n = 4;
spiralFill($m, $n, $a);
for ($i = 0; $i < $m; $i++)
{
    for ($j = 0; $j < $n; $j++)
    {
        echo ($a[$i][$j]);
        echo (" ");
    }
    echo ("\n");
}
  
// This code is contributed 
// by Shivi_Aggarwal
?>

chevron_right



Output:

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

Time complexity: O(m * n)

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.