Print a given matrix in anticlockwise spiral form

Anticlockwise spiral form of matrix

Here given code implementation process.

// Java program For
// Print a given matrix in anticlockwise spiral form
public class Matrix
{

  public void  spiral ( int [][]data , 
    int startRow , 
    int startCol , 
    int endRow , 
    int endCol , 
    int element)
  {
     // Top to bottom direction
    for (int i = startRow; i <= endRow && element>0; ++i , element--)
    {
      System.out.print("  "+data[i][startRow]);
    }
    // Left to right direction
    for (int i = startRow+1; i <= endCol  && element>0 ; ++i , element--)
    {
      System.out.print("  "+ data[endRow][i]);
    }
    // Bottomt to top direction
    for (int i = endRow-1; i >startRow  && element>0; --i , element--)
    {
      System.out.print("  "+ data[i][endCol]);
    }
    // Bottom to top direction
    for (int i = endCol; i > startCol  && element>0; --i , element--)
    {
      System.out.print("  "+ data[startRow][i]);
    }
  
    if (startRow+1 <= endRow-1 && element > 0)
    {
      // Recursive call
      spiral(data , startRow+1 , startCol+1 , endRow-1 , endCol-1 , element);
    }
    
  }

    public static void main(String[] args) 
    {
        Matrix task = new Matrix();
        // matrix elements
        int [][]matrix = {
            {1 ,  2 ,  3 ,  4 ,  5 ,  6} , 
            {22 , 23 , 24 , 25 , 26 , 7} , 
            {21 , 36 , 37 , 38 , 27 , 8} , 
            {20 , 35 , 42 , 39 , 28 , 9} , 
            {19 , 34 , 41 , 40 , 29 , 10} , 
            {18 , 33 , 32 , 31 , 30 , 11} , 
            {17 , 16 , 15 , 14 , 13 , 12} , 
        };
        int row =  matrix.length;
        int col =  matrix[0].length;
        // Get the size of array elements
        int element=row*col;
        // Print result
        task.spiral ( matrix , 0 , 0 , row-1 , col-1 , element) ; 
    }
}

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
// Include namespace system
using System;
// Csharp program For
// Print a given matrix in anticlockwise spiral form
public class Matrix
{
    public void spiral(int[, ] data, int startRow, 
  int startCol, int endRow, int endCol, int element)
    {
        // Top to bottom direction
        for (var i = startRow; i <= endRow && element > 0; ++i, element--)
        {
            Console.Write("  " + data[i, startRow].ToString());
        }
        // Left to right direction
        for (var i = startRow + 1; i <= endCol && element > 0; ++i, element--)
        {
            Console.Write("  " + data[endRow, i].ToString());
        }
        // Bottomt to top direction
        for (var i = endRow - 1; i > startRow && element > 0; --i, element--)
        {
            Console.Write("  " + data[i, endCol].ToString());
        }
        for (var i = endCol; i > startCol && element > 0; --i, element--)
        {
            Console.Write("  " + data[startRow, i].ToString());
        }
        // Bottom to top direction
        if (startRow + 1 <= endRow - 1 && element > 0)
        {
            // Recursive call
            this.spiral(data, startRow + 1, startCol + 1, 
                        endRow - 1, endCol - 1, element);
        }
    }
    public static void Main(String[] args)
    {
        var task = new Matrix();
        // matrix elements
        int[, ] matrix = {
            {
                1 , 2 , 3 , 4 , 5 , 6
            },
            {
                22 , 23 , 24 , 25 , 26 , 7
            },
            {
                21 , 36 , 37 , 38 , 27 , 8
            },
            {
                20 , 35 , 42 , 39 , 28 , 9
            },
            {
                19 , 34 , 41 , 40 , 29 , 10
            },
            {
                18 , 33 , 32 , 31 , 30 , 11
            },
            {
                17 , 16 , 15 , 14 , 13 , 12
            }
        };
        var row = matrix.GetLength(0);
        var col = matrix.GetLength(1);
        // Get the size of array elements
        var element = row * col;
        // Print result
        task.spiral(matrix, 0, 0, row - 1, col - 1, element);
    }
}

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
// Include header file
#include <iostream>
#include <vector>
using namespace std;
// C++ program For
// Print a given matrix in anticlockwise spiral form
class Matrix
{
    public: void spiral(vector<vector<int>> data, int startRow, int startCol, int endRow, int endCol, int element)
    {
        // Top to bottom direction
        for (int i = startRow; i <= endRow && element > 0; ++i, element--)
        {
            cout << "  " << data[i][startRow];
        }
        // Left to right direction
        for (int i = startRow + 1; i <= endCol && element > 0; ++i, element--)
        {
            cout << "  " << data[endRow][i];
        }
        // Bottomt to top direction
        for (int i = endRow - 1; i > startRow && element > 0; --i, element--)
        {
            cout << "  " << data[i][endCol];
        }
        // Bottom to top direction
        for (int i = endCol; i > startCol && element > 0; --i, element--)
        {
            cout << "  " << data[startRow][i];
        }
      
        if (startRow + 1 <= endRow - 1 && element > 0)
        {
            // Recursive call
            this->spiral(data, startRow + 1, startCol + 1, endRow - 1, endCol - 1, element);
        }
    }
};
int main()
{
    Matrix *task = new Matrix();
    // matrix elements
    vector<vector<int>> matrix {
        {
            1 , 2 , 3 , 4 , 5 , 6
        } , {
            22 , 23 , 24 , 25 , 26 , 7
        } , {
            21 , 36 , 37 , 38 , 27 , 8
        } , {
            20 , 35 , 42 , 39 , 28 , 9
        } , {
            19 , 34 , 41 , 40 , 29 , 10
        } , {
            18 , 33 , 32 , 31 , 30 , 11
        } , {
            17 , 16 , 15 , 14 , 13 , 12
        }
    };
    int row = matrix.size();
    int col = matrix[0].size();
    // Get the size of array elements
    int element = row * col;
    // Print result
    task->spiral(matrix, 0, 0, row - 1, col - 1, element);
    return 0;
}

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
/*
   C Program 
   Print a given matrix in anticlockwise spiral form
*/
#include <stdio.h>

#define ROW 7
#define COL 6
void spiral(int data[ROW][COL], int s_row, int s_col, int e_row, int e_col, int element)
{
    // Top to bottom direction
    for (int i = s_row; i <= e_row && element > 0; ++i, element--)
    {
        printf("%3d", data[i][s_row]);
    }
    // Left to right direction
    for (int i = s_row + 1; i <= e_col && element > 0; ++i, element--)
    {
        printf("%3d", data[e_row][i]);
    }
    // Bottomt to top direction
    for (int i = e_row - 1; i > s_row && element > 0; --i, element--)
    {
        printf("%3d", data[i][e_col]);
    }
    // Bottom to top direction
    for (int i = e_col; i > s_col && element > 0; --i, element--)
    {
        printf("%3d", data[s_row][i]);
    }
    if (s_row + 1 <= e_row - 1 && element > 0)
    {
        // Recursive call
        spiral(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element);
    }
}
int main()
{
    int arr[ROW][COL]= {
        {1 ,  2 ,  3 ,  4 ,  5 ,  6} , 
        {22 , 23 , 24 , 25 , 26 , 7} , 
        {21 , 36 , 37 , 38 , 27 , 8} , 
        {20 , 35 , 42 , 39 , 28 , 9} , 
        {19 , 34 , 41 , 40 , 29 , 10} , 
        {18 , 33 , 32 , 31 , 30 , 11} , 
        {17 , 16 , 15 , 14 , 13 , 12} , 
    };
    // Get number of element
    int element = COL * ROW;
    // print result
    spiral(arr, 0, 0, ROW - 1, COL - 1, element);
    return 0;
}

Output

  1 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2 23 36 35 34 33 32 31 30 29 28 27 26 25 24 37 42 41 40 39 38
package main
import "fmt"
// Go program For
// Print a given matrix in anticlockwise spiral form

func spiral(data[][] int, startRow int, startCol int, endRow int, 
    endCol int, n int) {
    
        var i int = startRow
        var element int = n
        // Top to bottom direction
        for i <= endRow && element > 0 {
            fmt.Print("  ", data[i][startRow])
            i += 1;
            element -= 1;
        }
        i = startRow + 1;
        // Left to right direction
        for i <= endCol && element > 0 {
            fmt.Print("  ", data[endRow][i])
            i += 1
            element -= 1
        }
        i = endRow - 1
        // Bottomt to top direction
        for i > startRow && element > 0 {
            fmt.Print("  ", data[i][endCol])
            i -= 1
            element -= 1
        }
        i = endCol;
        for i > startCol && element > 0 {
            fmt.Print("  ", data[startRow][i])
            i -= 1
            element -= 1
        }
        if startRow + 1 <= endRow - 1 && element > 0 {
            // Recursive call
            spiral(data, startRow + 1, startCol + 1, 
                endRow - 1, endCol - 1, element)
        }
}
func main() {
    
    // matrix elements
    var matrix = [][] int {
            {1 ,  2 ,  3 ,  4 ,  5 ,  6} , 
            {22 , 23 , 24 , 25 , 26 , 7} , 
            {21 , 36 , 37 , 38 , 27 , 8} , 
            {20 , 35 , 42 , 39 , 28 , 9} , 
            {19 , 34 , 41 , 40 , 29 , 10} , 
            {18 , 33 , 32 , 31 , 30 , 11} , 
            {17 , 16 , 15 , 14 , 13 , 12} , 
    };
    var row int = len(matrix)
    var col int = len(matrix[0])
    // Get the size of array elements
    var element int = row * col
    // Print result
    spiral(matrix, 0, 0, row - 1, col - 1, element)
}

Output

java/Matrix.java:1: error: ';' expected
package main
            ^
java/Matrix.java:2: error: <identifier> expected
import "fmt"
      ^
java/Matrix.java:6: error: ';' expected
func spiral(data[][] int, startRow int, startCol int, endRow int,
    ^
java/Matrix.java:16: error: class, interface, or enum expected
            element -= 1;
            ^
java/Matrix.java:17: error: class, interface, or enum expected
        }
        ^
java/Matrix.java:20: error: class, interface, or enum expected
        while (i <= endCol && element > 0)
        ^
java/Matrix.java:24: error: class, interface, or enum expected
            element -= 1;
            ^
java/Matrix.java:25: error: class, interface, or enum expected
        }
        ^
java/Matrix.java:28: error: class, interface, or enum expected
        while (i > startRow && element > 0)
        ^
java/Matrix.java:32: error: class, interface, or enum expected
            element -= 1;
            ^
java/Matrix.java:33: error: class, interface, or enum expected
        }
        ^
java/Matrix.java:35: error: class, interface, or enum expected
        while (i > startCol && element > 0)
        ^
java/Matrix.java:58: error: class, interface, or enum expected
    var row int = len(matrix)
    ^
13 errors
<?php
// Php program For
// Print a given matrix in anticlockwise spiral form
class Matrix
{
    public  function spiral($data, $startRow, 
                             $startCol, $endRow, $endCol, $element)
    {
        // Top to bottom direction
        for ($i = $startRow; $i <= $endRow && $element > 0; ++$i, $element--)
        {
            printf("%s", "  ".strval($data[$i][$startRow]));
        }
        // Left to right direction
        for ($i = $startRow + 1; $i <= $endCol && $element > 0; ++$i, $element--)
        {
            printf("%s", "  ".strval($data[$endRow][$i]));
        }
        // Bottomt to top direction
        for ($i = $endRow - 1; $i > $startRow && $element > 0; --$i, $element--)
        {
            printf("%s", "  ".strval($data[$i][$endCol]));
        }
        for ($i = $endCol; $i > $startCol && $element > 0; --$i, $element--)
        {
            printf("%s", "  ".strval($data[$startRow][$i]));
        }
        // Bottom to top direction
        if ($startRow + 1 <= $endRow - 1 && $element > 0)
        {
            // Recursive call
            $this->spiral($data, $startRow + 1, $startCol + 1, $endRow - 1, $endCol - 1, $element);
        }
    }
    public static
    function main($args)
    {
        $task = new Matrix();
        // matrix elements
        $matrix = array(
          array(1, 2, 3, 4, 5, 6), 
          array(22, 23, 24, 25, 26, 7), 
          array(21, 36, 37, 38, 27, 8), 
          array(20, 35, 42, 39, 28, 9), 
          array(19, 34, 41, 40, 29, 10), 
          array(18, 33, 32, 31, 30, 11), 
          array(17, 16, 15, 14, 13, 12)
        );
        $row = count($matrix);
        $col = count($matrix[0]);
        // Get the size of array elements
        $element = $row * $col;
        // Print result
        $task->spiral($matrix, 0, 0, $row - 1, $col - 1, $element);
    }
}
Matrix::main(array());

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
// Node JS program For
// Print a given matrix in anticlockwise spiral form
class Matrix
{
    spiral(data, startRow, startCol, endRow, endCol, element)
    {
        // Top to bottom direction
        for (var i = startRow; i <= endRow && element > 0; ++i, element--)
        {
            process.stdout.write("  " + data[i][startRow]);
        }
        // Left to right direction
        for (var i = startRow + 1; i <= endCol && element > 0; ++i, element--)
        {
            process.stdout.write("  " + data[endRow][i]);
        }
        // Bottomt to top direction
        for (var i = endRow - 1; i > startRow && element > 0; --i, element--)
        {
            process.stdout.write("  " + data[i][endCol]);
        }
        for (var i = endCol; i > startCol && element > 0; --i, element--)
        {
            process.stdout.write("  " + data[startRow][i]);
        }
        // Bottom to top direction
        if (startRow + 1 <= endRow - 1 && element > 0)
        {
            // Recursive call
            this.spiral(data, startRow + 1, startCol + 1, 
                        endRow - 1, endCol - 1, element);
        }
    }
}

function main()
{
    var task = new Matrix();
    // matrix elements
    var matrix = [
        [1, 2, 3, 4, 5, 6],
        [22, 23, 24, 25, 26, 7],
        [21, 36, 37, 38, 27, 8],
        [20, 35, 42, 39, 28, 9],
        [19, 34, 41, 40, 29, 10],
        [18, 33, 32, 31, 30, 11],
        [17, 16, 15, 14, 13, 12]
    ];
    var row = matrix.length;
    var col = matrix[0].length;
    // Get the size of array elements
    var element = row * col;
    // Print result
    task.spiral(matrix, 0, 0, row - 1, col - 1, element);
}
// Start program execution
main();

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
#  Python 3 program For
#  Print a given matrix in anticlockwise spiral form
class Matrix :
    def spiral(self, data, startRow, startCol, endRow, endCol, element) :
        i = startRow
        #  Top to bottom direction
        while (i <= endRow and element > 0) :
            print(" ", data[i][startRow], end = "")
            i += 1
            element -= 1
        
        i = startRow + 1
        #  Left to right direction
        while (i <= endCol and element > 0) :
            print(" ", data[endRow][i], end = "")
            i += 1
            element -= 1
        
        i = endRow - 1
        #  Bottomt to top direction
        while (i > startRow and element > 0) :
            print(" ", data[i][endCol], end = "")
            i -= 1
            element -= 1
        
        i = endCol
        while (i > startCol and element > 0) :
            print(" ", data[startRow][i], end = "")
            i -= 1
            element -= 1
        
        #  Bottom to top direction
        if (startRow + 1 <= endRow - 1 and element > 0) :
            #  Recursive call
            self.spiral(data, startRow + 1, startCol + 1, 
                        endRow - 1, endCol - 1, element)
        
    

def main() :
    task = Matrix()
    #  matrix elements
    matrix = [
        [1, 2, 3, 4, 5, 6],
        [22, 23, 24, 25, 26, 7],
        [21, 36, 37, 38, 27, 8],
        [20, 35, 42, 39, 28, 9],
        [19, 34, 41, 40, 29, 10],
        [18, 33, 32, 31, 30, 11],
        [17, 16, 15, 14, 13, 12]
    ]
    row = len(matrix)
    col = len(matrix[0])
    #  Get the size of list elements
    element = row * col
    #  Print result
    task.spiral(matrix, 0, 0, row - 1, col - 1, element)

if __name__ == "__main__": main()

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
#  Ruby program For
#  Print a given matrix in anticlockwise spiral form
class Matrix 
    def spiral(data, startRow, startCol, endRow, endCol, element) 
        i = startRow
        #  Top to bottom direction
        while (i <= endRow && element > 0) 
            print("  ", data[i][startRow])
            i += 1
            element -= 1
        end

        i = startRow + 1
        #  Left to right direction
        while (i <= endCol && element > 0) 
            print("  ", data[endRow][i])
            i += 1
            element -= 1
        end

        i = endRow - 1
        #  Bottomt to top direction
        while (i > startRow && element > 0) 
            print("  ", data[i][endCol])
            i -= 1
            element -= 1
        end

        i = endCol
        while (i > startCol && element > 0) 
            print("  ", data[startRow][i])
            i -= 1
            element -= 1
        end

        #  Bottom to top direction
        if (startRow + 1 <= endRow - 1 && element > 0) 
            #  Recursive call
            self.spiral(data, startRow + 1, startCol + 1, 
                        endRow - 1, endCol - 1, element)
        end

    end

end

def main() 
    task = Matrix.new()
    #  matrix elements
    matrix = [
        [1, 2, 3, 4, 5, 6],
        [22, 23, 24, 25, 26, 7],
        [21, 36, 37, 38, 27, 8],
        [20, 35, 42, 39, 28, 9],
        [19, 34, 41, 40, 29, 10],
        [18, 33, 32, 31, 30, 11],
        [17, 16, 15, 14, 13, 12]
    ]
    row = matrix.length
    col = matrix[0].length
    #  Get the size of array elements
    element = row * col
    #  Print result
    task.spiral(matrix, 0, 0, row - 1, col - 1, element)
end

main()

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
// Scala program For
// Print a given matrix in anticlockwise spiral form
class Matrix()
{
    def spiral(data: Array[Array[Int]], startRow: Int, 
      startCol: Int, endRow: Int, endCol: Int,  n: Int): Unit = {
        var i: Int = startRow;
        var element: Int = n;
        // Top to bottom direction
        while (i <= endRow && element > 0)
        {
            print("  " + data(i)(startRow));
            i += 1;
            element -= 1;
        }
        i = startRow + 1;
        // Left to right direction
        while (i <= endCol && element > 0)
        {
            print("  " + data(endRow)(i));
            i += 1;
            element -= 1;
        }
        i = endRow - 1;
        // Bottomt to top direction
        while (i > startRow && element > 0)
        {
            print("  " + data(i)(endCol));
            i -= 1;
            element -= 1;
        }
        i = endCol;
        while (i > startCol && element > 0)
        {
            print("  " + data(startRow)(i));
            i -= 1;
            element -= 1;
        }
        // Bottom to top direction
        if (startRow + 1 <= endRow - 1 && element > 0)
        {
            // Recursive call
            spiral(data, startRow + 1, startCol + 1, 
                   endRow - 1, endCol - 1, element);
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Matrix = new Matrix();
        // matrix elements
        var matrix: Array[Array[Int]] = Array(
          Array(1, 2, 3, 4, 5, 6), 
          Array(22, 23, 24, 25, 26, 7), 
          Array(21, 36, 37, 38, 27, 8), 
          Array(20, 35, 42, 39, 28, 9), 
          Array(19, 34, 41, 40, 29, 10), 
          Array(18, 33, 32, 31, 30, 11), 
          Array(17, 16, 15, 14, 13, 12)
        );
        var row: Int = matrix.length;
        var col: Int = matrix(0).length;
        // Get the size of array elements
        var element: Int = row * col;
        // Print result
        task.spiral(matrix, 0, 0, row - 1, col - 1, element);
    }
}

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
import Foundation
// Swift 4 program For
// Print a given matrix in anticlockwise spiral form
class Matrix
{
    func spiral(_ data: [
        [Int]
    ], _ startRow: Int, _ startCol: Int, 
      _ endRow: Int, _ endCol: Int, _ n: Int)
    {
        
            var i: Int = startRow;
            var element: Int = n;
            // Top to bottom direction
            while (i <= endRow && element > 0)
            {
                print("  "
                    + String(data[i][startRow]), terminator: "");
                i += 1;
                element -= 1;
            }
    
            i = startRow + 1;
            // Left to right direction
            while (i <= endCol && element > 0)
            {
                print("  "
                    + String(data[endRow][i]), terminator: "");
                i += 1;
                element -= 1;
            }
            i = endRow - 1;
            // Bottomt to top direction
            while (i > startRow && element > 0)
            {
                print("  "
                    + String(data[i][endCol]), terminator: "");
                i -= 1;
                element -= 1;
            }
            i = endCol;
            while (i > startCol && element > 0)
            {
                print("  "
                    + String(data[startRow][i]), terminator: "");
                i -= 1;
                element -= 1;
            }
        
        // Bottom to top direction
        if (startRow + 1 <= endRow - 1 && element > 0)
        {
            // Recursive call
            self.spiral(data, startRow + 1, 
                        startCol + 1, endRow - 1, 
                        endCol - 1, element);
        }
    }
    static func main(_ args: [String])
    {
        let task: Matrix? = Matrix();
        // matrix elements
        let matrix: [
            [Int]
        ] = [
            [1, 2, 3, 4, 5, 6],
            [22, 23, 24, 25, 26, 7],
            [21, 36, 37, 38, 27, 8],
            [20, 35, 42, 39, 28, 9],
            [19, 34, 41, 40, 29, 10],
            [18, 33, 32, 31, 30, 11],
            [17, 16, 15, 14, 13, 12]
        ];
        let row: Int = matrix.count;
        let col: Int = matrix[0].count;
        // Get the size of array elements
        let element: Int = row * col;
        // Print result
        task!.spiral(matrix, 0, 0, row - 1, col - 1, element);
    }
}
Matrix.main([String]());

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38
// Kotlin program For
// Print a given matrix in anticlockwise spiral form
class Matrix
{
    fun spiral(data: Array < Array < Int >> , 
                startRow: Int, startCol: Int, endRow: Int, 
                endCol: Int, n: Int): Unit
    {
        var i: Int = startRow;
        var element: Int = n;
        // Top to bottom direction
        while (i <= endRow && element > 0)
        {
            print("  " + data[i][startRow]);
            i += 1;
            element -= 1;
        }
        i = startRow + 1;
        // Left to right direction
        while (i <= endCol && element > 0)
        {
            print("  " + data[endRow][i]);
            i += 1;
            element -= 1;
        }
        i = endRow - 1;
        // Bottomt to top direction
        while (i > startRow && element > 0)
        {
            print("  " + data[i][endCol]);
            i -= 1;
            element -= 1;
        }
        i = endCol;
        while (i > startCol && element > 0)
        {
            print("  " + data[startRow][i]);
            i -= 1;
            element -= 1;
        }
        // Bottom to top direction
        if (startRow + 1 <= endRow - 1 && element > 0)
        {
            // Recursive call
            this.spiral(data, startRow + 1, 
                        startCol + 1, endRow - 1, 
                        endCol - 1, element);
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Matrix = Matrix();
    // matrix elements
    val matrix: Array < Array < Int >> = arrayOf(
      arrayOf(1, 2, 3, 4, 5, 6), 
      arrayOf(22, 23, 24, 25, 26, 7), 
      arrayOf(21, 36, 37, 38, 27, 8), 
      arrayOf(20, 35, 42, 39, 28, 9), 
      arrayOf(19, 34, 41, 40, 29, 10), 
      arrayOf(18, 33, 32, 31, 30, 11), 
      arrayOf(17, 16, 15, 14, 13, 12)
    );
    val row: Int = matrix.count();
    val col: Int = matrix[0].count();
    // Get the size of array elements
    val element: Int = row * col;
    // Print result
    task.spiral(matrix, 0, 0, row - 1, col - 1, element);
}

Output

  1  22  21  20  19  18  17  16  15  14  13  12  11  10  9  8  7  6  5  4  3  2  23  36  35  34  33  32  31  30  29  28  27  26  25  24  37  42  41  40  39  38


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved