Print Half clockwise and Half anticlockwise spiral view of a matrix
The problem is to print the half-clockwise and half-anticlockwise spiral views of a given matrix. In the half-clockwise spiral view, elements are printed in a spiral manner starting from the top-left corner, moving to the right, then down, left, and so on. In the half-anticlockwise spiral view, elements are printed in a spiral manner starting from the top-right corner, moving down, left, up, and so on. The goal is to print these two spiral views of the matrix.
Example
Consider the following 7x6 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
The half-clockwise spiral view starts from the top-left corner and goes in the following order:
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
. The half-anticlockwise spiral view starts
from the top-right corner and goes in the following order:
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
.
Idea to Solve the Problem

To print the half-clockwise and half-anticlockwise spiral views of the matrix, we can follow these steps:
- Define a class
MyMatrix
to handle matrix operations. - Implement the
clockwise
function to print the half-clockwise spiral view: a. Loop through each side of the spiral: i. Print the elements from left to right along the top side. ii. Print the elements from top to bottom along the right side. iii. Print the elements from right to left along the bottom side. iv. Print the elements from bottom to top along the left side. v. Decrease the value ofk
(number of remaining elements to print). b. If there are still elements to print and the inner spiral is not yet covered, recursively call theclockwise
function with the updated boundaries. - Implement the
anticlockwise
function similarly to theclockwise
function to print the half-anticlockwise spiral view. - In the
spiral_view
function, determine whether the matrix has more than 2 columns: a. If it has more than 2 columns, check if the number of columns is even or odd. b. Call theclockwise
function and theanticlockwise
function accordingly to print the two spiral views. - Print the half-clockwise and half-anticlockwise spiral views of the matrix.
Pseudocode
clockwise(matrix, s_row, s_col, e_row, e_col, k):
# Print left to right
...
# Print top to down
...
# Print bottom right to bottom left
...
# Print bottom left to top
...
if s_row + 1 <= e_row - 1 and k > 0:
clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
anticlockwise(matrix, s_row, s_col, e_row, e_col, k):
# Print top to down
...
# Print left to right
...
# Print bottom left to top
...
# Print top right to left
...
if s_row + 1 <= e_row - 1 and k > 0:
anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
spiral_view(matrix):
if this.cols > 2:
if this.cols % 2 == 0:
# Print clockwise and anticlockwise views
...
else:
# Print clockwise and anticlockwise views
...
else if this.cols == 2:
# Print clockwise and anticlockwise views
...
Code Solution
Here given code implementation process.
/*
C Program
+ Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
#include<stdio.h>
#define ROW 7
#define COL 6
void clockwise(int data[ROW][COL],
int s_row,
int s_col,
int e_row,
int e_col,
int k)
{
//Left to right
for (int i = s_col; i <=e_col && k > 0 ; ++i,k--)
{
printf("%4d",data[s_row][i] );
}
//Top to down
for (int i = s_row+1; i <=e_row && k > 0 ; ++i,k--)
{
printf("%4d",data[i][e_col] );
}
//Bottom right to bottom-left
for (int i = e_col-1; i >=s_col && k > 0 ; --i,k--)
{
printf("%4d",data[e_row][i] );
}
//Bottom left to top
for (int i =e_row-1 ; i > s_row && k > 0 ; --i,k--)
{
printf("%4d",data[i][s_row] );
}
if(s_row+1 <= e_row-1 && k > 0)
{
//Recursive call
clockwise(data,s_row+1,s_col+1,e_row-1,e_col-1,k);
}
}
void anticlockwise(int data[ROW][COL],
int s_row,
int s_col,
int e_row,
int e_col,
int k)
{
//Top to down
for (int i = s_row; i <=e_row && k > 0 ; ++i,k--)
{
printf("%4d",data[i][s_col] );
}
//Left to right
for (int i = s_col+1; i <=e_col && k > 0 ; ++i,k--)
{
printf("%4d",data[e_row][i] );
}
//Bottom left to top
for (int i =e_row-1 ; i >= s_row && k > 0 ; --i,k--)
{
printf("%4d",data[i][e_col] );
}
//top right to left
for (int i = e_col-1; i > s_col && k > 0 ; --i,k--)
{
printf("%4d",data[s_row][i] );
}
if(s_row+1 <= e_row-1 && k > 0)
{
//Recursive call
anticlockwise(data,s_row+1,s_col+1,e_row-1,e_col-1,k);
}
}
void spiral_view(int arr[ROW][COL])
{
if(COL>2)
{
//When exists more than 2 columns
if(COL%2==0)
{
//When the size of column are Even
printf("\n Clockwise \n");
clockwise(arr,0,0,ROW-1,(COL/2)-1,(ROW*COL)/2);
printf("\n Anticlockwise \n");
anticlockwise(arr,0,(COL/2),ROW-1,COL-1,(ROW*COL)/2);
}
else
{
//When the size of column are Odd
printf("\n Clockwise \n");
clockwise(arr,0,0,ROW-1,(COL/2),ROW*(COL+1)/2);
printf("\n Anticlockwise \n");
anticlockwise(arr,0,(COL/2),ROW-1,COL-1,ROW*(COL+1)/2 );
}
}
else if(COL == 2)
{
//When exists only 2 columns
printf("\n Clockwise \n");
clockwise(arr,0,0,ROW-1,COL-1,(ROW*COL));
printf("\n Anticlockwise \n");
anticlockwise(arr,0,0,ROW-1,COL-1,(ROW*COL));
}
}
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},
};
spiral_view(arr);
return 0;
}
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
/*
C++ Program
Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
#include<iostream>
#define ROW 7
#define COL 6
using namespace std;
class MyMatrix {
public:
int rows;
int cols;
MyMatrix() {
//Get matrix size
this->rows = ROW;
this->cols = COL;
}
void clockwise(int matrix[][COL], int s_row, int s_col, int e_row, int e_col, int k) {
//Left to right
for (int i = s_col; i <= e_col && k > 0; ++i, k--) {
cout << " " << matrix[s_row][i];
}
//Top to down
for (int i = s_row + 1; i <= e_row && k > 0; ++i, k--) {
cout << " " << matrix[i][e_col];
}
//Bottom right to bottom-left
for (int i = e_col - 1; i >= s_col && k > 0; --i, k--) {
cout << " " << matrix[e_row][i];
}
//Bottom left to top
for (int i = e_row - 1; i > s_row && k > 0; --i, k--) {
cout << " " << matrix[i][s_row];
}
if (s_row + 1 <= e_row - 1 && k > 0) {
//Recursive call
this->clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
void anticlockwise(int matrix[][COL], int s_row, int s_col, int e_row, int e_col, int k) {
//Top to down
for (int i = s_row; i <= e_row && k > 0; ++i, k--) {
cout << " " << matrix[i][s_col];
}
//Left to right
for (int i = s_col + 1; i <= e_col && k > 0; ++i, k--) {
cout << " " << matrix[e_row][i];
}
//Bottom left to top
for (int i = e_row - 1; i >= s_row && k > 0; --i, k--) {
cout << " " << matrix[i][e_col];
}
//top right to left
for (int i = e_col - 1; i > s_col && k > 0; --i, k--) {
cout << " " << matrix[s_row][i];
}
if (s_row + 1 <= e_row - 1 && k > 0) {
//Recursive call
this->anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
void spiral_view(int matrix[][COL]) {
if (this->cols > 2) {
//When exists more than 2 columns
if (this->cols % 2 == 0) {
//When the size of column are Even
cout << "\n Clockwise \n";
this->clockwise(matrix, 0, 0, this->rows - 1, (this->cols / 2) - 1, (this->rows *this->cols) / 2);
cout << "\n Anticlockwise \n";
this->anticlockwise(matrix, 0, (this->cols / 2), this->rows - 1, this->cols - 1, (this->rows *this->cols) / 2);
} else {
//When the size of column are Odd
cout << "\n Clockwise \n";
this->clockwise(matrix, 0, 0, this->rows - 1, (this->cols / 2), this->rows *(this->cols + 1) / 2);
cout << "\n Anticlockwise \n";
this->anticlockwise(matrix, 0, (this->cols / 2), this->rows - 1, this->cols - 1, this->rows *(this->cols + 1) / 2);
}
} else
if (this->cols == 2) {
//When exists only 2 columns
cout << "\n Clockwise \n";
this->clockwise(matrix, 0, 0, this->rows - 1, this->cols - 1, (this->rows *this->cols));
cout << "\n Anticlockwise \n";
this->anticlockwise(matrix, 0, 0, this->rows - 1, this->cols - 1, (this->rows *this->cols));
}
}
};
int main() {
int matrix[][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
}
};
MyMatrix obj ;
obj.spiral_view(matrix);
return 0;
}
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
/*
Java Program
Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
public class MyMatrix {
public int rows;
public int cols;
public MyMatrix(int [][]matrix)
{
//Get matrix size
this.rows = matrix.length;
this.cols = matrix[0].length;
}
public void clockwise(int [][]matrix,
int s_row,
int s_col,
int e_row,
int e_col,
int k)
{
//Left to right
for (int i = s_col; i <=e_col && k > 0 ; ++i,k--)
{
System.out.print(" "+matrix[s_row][i] );
}
//Top to down
for (int i = s_row+1; i <=e_row && k > 0 ; ++i,k--)
{
System.out.print(" "+matrix[i][e_col] );
}
//Bottom right to bottom-left
for (int i = e_col-1; i >=s_col && k > 0 ; --i,k--)
{
System.out.print(" "+matrix[e_row][i] );
}
//Bottom left to top
for (int i =e_row-1 ; i > s_row && k > 0 ; --i,k--)
{
System.out.print(" "+matrix[i][s_row] );
}
if(s_row+1 <= e_row-1 && k > 0)
{
//Recursive call
clockwise(matrix,s_row+1,s_col+1,e_row-1,e_col-1,k);
}
}
public void anticlockwise(int [][]matrix,
int s_row,
int s_col,
int e_row,
int e_col,
int k)
{
//Top to down
for (int i = s_row; i <=e_row && k > 0 ; ++i,k--)
{
System.out.print(" "+matrix[i][s_col] );
}
//Left to right
for (int i = s_col+1; i <=e_col && k > 0 ; ++i,k--)
{
System.out.print(" "+matrix[e_row][i] );
}
//Bottom left to top
for (int i =e_row-1 ; i >= s_row && k > 0 ; --i,k--)
{
System.out.print(" "+matrix[i][e_col] );
}
//top right to left
for (int i = e_col-1; i > s_col && k > 0 ; --i,k--)
{
System.out.print(" "+matrix[s_row][i] );
}
if(s_row+1 <= e_row-1 && k > 0)
{
//Recursive call
anticlockwise(matrix,s_row+1,s_col+1,e_row-1,e_col-1,k);
}
}
public void spiral_view(int [][]matrix)
{
if(this.cols>2)
{
//When exists more than 2 columns
if(this.cols%2==0)
{
//When the size of column are Even
System.out.print("\n Clockwise \n");
clockwise(matrix,0,0,this.rows-1,(this.cols/2)-1,(this.rows*this.cols)/2);
System.out.print("\n Anticlockwise \n");
anticlockwise(matrix,0,(this.cols/2),this.rows-1,this.cols-1,(this.rows*this.cols)/2);
}
else
{
//When the size of column are Odd
System.out.print("\n Clockwise \n");
clockwise(matrix,0,0,this.rows-1,(this.cols/2),this.rows*(this.cols+1)/2);
System.out.print("\n Anticlockwise \n");
anticlockwise(matrix,0,(this.cols/2),this.rows-1,this.cols-1,this.rows*(this.cols+1)/2 );
}
}
else if(this.cols == 2)
{
//When exists only 2 columns
System.out.print("\n Clockwise \n");
clockwise(matrix,0,0,this.rows-1,this.cols-1,(this.rows*this.cols));
System.out.print("\n Anticlockwise \n");
anticlockwise(matrix,0,0,this.rows-1,this.cols-1,(this.rows*this.cols));
}
}
public static void main(String[] args)
{
//Define matrix element
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}
};
MyMatrix obj = new MyMatrix(matrix);
obj.spiral_view(matrix);
}
}
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
/*
C# Program
Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
using System;
public class MyMatrix {
int rows;
int cols;
MyMatrix(int[,] matrix) {
//Get matrix size
this.rows = matrix.GetLength(0);
this.cols = matrix.GetLength(1);
}
public void clockwise(int[,] matrix, int s_row, int s_col, int e_row, int e_col, int k) {
//Left to right
for (int i = s_col; i <= e_col && k > 0; ++i, k--) {
Console.Write(" " + matrix[s_row,i]);
}
//Top to down
for (int i = s_row + 1; i <= e_row && k > 0; ++i, k--) {
Console.Write(" " + matrix[i,e_col]);
}
//Bottom right to bottom-left
for (int i = e_col - 1; i >= s_col && k > 0; --i, k--) {
Console.Write(" " + matrix[e_row,i]);
}
//Bottom left to top
for (int i = e_row - 1; i > s_row && k > 0; --i, k--) {
Console.Write(" " + matrix[i,s_row]);
}
if (s_row + 1 <= e_row - 1 && k > 0) {
clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
public void anticlockwise(int[,] matrix, int s_row, int s_col, int e_row, int e_col, int k) {
//Top to down
for (int i = s_row; i <= e_row && k > 0; ++i, k--) {
Console.Write(" " + matrix[i,s_col]);
}
//Left to right
for (int i = s_col + 1; i <= e_col && k > 0; ++i, k--) {
Console.Write(" " + matrix[e_row,i]);
}
//Bottom left to top
for (int i = e_row - 1; i >= s_row && k > 0; --i, k--) {
Console.Write(" " + matrix[i,e_col]);
}
//top right to left
for (int i = e_col - 1; i > s_col && k > 0; --i, k--) {
Console.Write(" " + matrix[s_row,i]);
}
if (s_row + 1 <= e_row - 1 && k > 0) {
anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
public void spiral_view(int[,] matrix) {
if (this.cols > 2) {
//When exists more than 2 columns
if (this.cols % 2 == 0) {
Console.Write("\n Clockwise \n");
clockwise(matrix, 0, 0, this.rows - 1, (this.cols / 2) - 1, (this.rows * this.cols) / 2);
Console.Write("\n Anticlockwise \n");
anticlockwise(matrix, 0, (this.cols / 2), this.rows - 1, this.cols - 1, (this.rows * this.cols) / 2);
} else {
Console.Write("\n Clockwise \n");
clockwise(matrix, 0, 0, this.rows - 1, (this.cols / 2), this.rows * (this.cols + 1) / 2);
Console.Write("\n Anticlockwise \n");
anticlockwise(matrix, 0, (this.cols / 2), this.rows - 1, this.cols - 1, this.rows * (this.cols + 1) / 2);
}
} else
if (this.cols == 2) {
Console.Write("\n Clockwise \n");
clockwise(matrix, 0, 0, this.rows - 1, this.cols - 1, (this.rows * this.cols));
Console.Write("\n Anticlockwise \n");
anticlockwise(matrix, 0, 0, this.rows - 1, this.cols - 1, (this.rows * this.cols));
}
}
public static void Main(String[] args) {
int[,]
//Define matrix element
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
}
};
MyMatrix obj = new MyMatrix(matrix);
obj.spiral_view(matrix);
}
}
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
<?php
/*
Php Program
Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
class MyMatrix {
public $rows;
public $cols;
function __construct($matrix) {
//Get matrix size
$this->rows = count($matrix);
$this->cols = count($matrix[0]);
}
public function clockwise($matrix, $s_row, $s_col, $e_row, $e_col, $k) {
//Left to right
for ($i = $s_col; $i <= $e_col && $k > 0; ++$i, $k--) {
echo(" ". $matrix[$s_row][$i]);
}
//Top to down
for ($i = $s_row + 1; $i <= $e_row && $k > 0; ++$i, $k--) {
echo(" ". $matrix[$i][$e_col]);
}
//Bottom right to bottom-left
for ($i = $e_col - 1; $i >= $s_col && $k > 0; --$i, $k--) {
echo(" ". $matrix[$e_row][$i]);
}
//Bottom left to top
for ($i = $e_row - 1; $i > $s_row && $k > 0; --$i, $k--) {
echo(" ". $matrix[$i][$s_row]);
}
if ($s_row + 1 <= $e_row - 1 && $k > 0) {
//Recursive call
$this->clockwise($matrix, $s_row + 1, $s_col + 1, $e_row - 1, $e_col - 1, $k);
}
}
public function anticlockwise($matrix, $s_row, $s_col, $e_row, $e_col, $k) {
//Top to down
for ($i = $s_row; $i <= $e_row && $k > 0; ++$i, $k--) {
echo(" ". $matrix[$i][$s_col]);
}
//Left to right
for ($i = $s_col + 1; $i <= $e_col && $k > 0; ++$i, $k--) {
echo(" ". $matrix[$e_row][$i]);
}
//Bottom left to top
for ($i = $e_row - 1; $i >= $s_row && $k > 0; --$i, $k--) {
echo(" ". $matrix[$i][$e_col]);
}
//top right to left
for ($i = $e_col - 1; $i > $s_col && $k > 0; --$i, $k--) {
echo(" ". $matrix[$s_row][$i]);
}
if ($s_row + 1 <= $e_row - 1 && $k > 0) {
//Recursive call
$this->anticlockwise($matrix, $s_row + 1, $s_col + 1, $e_row - 1, $e_col - 1, $k);
}
}
public function spiral_view($matrix) {
if ($this->cols > 2) {
//When exists more than 2 columns
if ($this->cols % 2 == 0) {
//When the size of column are Even
echo("\n Clockwise \n");
$this->clockwise($matrix, 0, 0, $this->rows - 1, (intval($this->cols / 2)) - 1, intval(($this->rows *$this->cols) / 2));
echo("\n Anticlockwise \n");
$this->anticlockwise($matrix, 0, (intval($this->cols / 2)), $this->rows - 1, $this->cols - 1, intval(($this->rows *$this->cols) / 2));
} else {
//When the size of column are Odd
echo("\n Clockwise \n");
$this->clockwise($matrix, 0, 0, $this->rows - 1, (intval($this->cols / 2)), intval($this->rows *($this->cols + 1) / 2));
echo("\n Anticlockwise \n");
$this->anticlockwise($matrix, 0, (intval($this->cols / 2)), $this->rows - 1, $this->cols - 1, intval($this->rows *($this->cols + 1) / 2));
}
} else
if ($this->cols == 2) {
//When exists only 2 columns
echo("\n Clockwise \n");
$this->clockwise($matrix, 0, 0, $this->rows - 1, $this->cols - 1, ($this->rows *$this->cols));
echo("\n Anticlockwise \n");
$this->anticlockwise($matrix, 0, 0, $this->rows - 1, $this->cols - 1, ($this->rows *$this->cols));
}
}
}
function main() {
//Define matrix element
$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));
$obj = new MyMatrix($matrix);
$obj->spiral_view($matrix);
}
main();
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
/*
Node Js Program
Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
class MyMatrix {
;;
constructor(matrix) {
//Get matrix size
this.rows = matrix.length;
this.cols = matrix[0].length;
}
clockwise(matrix, s_row, s_col, e_row, e_col, k) {
//Left to right
for (var i = s_col; i <= e_col && k > 0; ++i, k--) {
process.stdout.write(" " + matrix[s_row][i]);
}
//Top to down
for (var i = s_row + 1; i <= e_row && k > 0; ++i, k--) {
process.stdout.write(" " + matrix[i][e_col]);
}
//Bottom right to bottom-left
for (var i = e_col - 1; i >= s_col && k > 0; --i, k--) {
process.stdout.write(" " + matrix[e_row][i]);
}
//Bottom left to top
for (var i = e_row - 1; i > s_row && k > 0; --i, k--) {
process.stdout.write(" " + matrix[i][s_row]);
}
if (s_row + 1 <= e_row - 1 && k > 0) {
//Recursive call
this.clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
anticlockwise(matrix, s_row, s_col, e_row, e_col, k) {
//Top to down
for (var i = s_row; i <= e_row && k > 0; ++i, k--) {
process.stdout.write(" " + matrix[i][s_col]);
}
//Left to right
for (var i = s_col + 1; i <= e_col && k > 0; ++i, k--) {
process.stdout.write(" " + matrix[e_row][i]);
}
//Bottom left to top
for (var i = e_row - 1; i >= s_row && k > 0; --i, k--) {
process.stdout.write(" " + matrix[i][e_col]);
}
//top right to left
for (var i = e_col - 1; i > s_col && k > 0; --i, k--) {
process.stdout.write(" " + matrix[s_row][i]);
}
if (s_row + 1 <= e_row - 1 && k > 0) {
//Recursive call
this.anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
spiral_view(matrix) {
if (this.cols > 2) {
//When exists more than 2 columns
if (this.cols % 2 == 0) {
//When the size of column are Even
process.stdout.write("\n Clockwise \n");
this.clockwise(matrix, 0, 0, this.rows - 1, (parseInt(this.cols / 2)) - 1, parseInt((this.rows *this.cols) / 2));
process.stdout.write("\n Anticlockwise \n");
this.anticlockwise(matrix, 0, (parseInt(this.cols / 2)), this.rows - 1, this.cols - 1, parseInt((this.rows *this.cols) / 2));
} else {
//When the size of column are Odd
process.stdout.write("\n Clockwise \n");
this.clockwise(matrix, 0, 0, this.rows - 1, (parseInt(this.cols / 2)), parseInt(this.rows *(this.cols + 1) / 2));
process.stdout.write("\n Anticlockwise \n");
this.anticlockwise(matrix, 0, (parseInt(this.cols / 2)), this.rows - 1, this.cols - 1, parseInt(this.rows *(this.cols + 1) / 2));
}
} else
if (this.cols == 2) {
//When exists only 2 columns
process.stdout.write("\n Clockwise \n");
this.clockwise(matrix, 0, 0, this.rows - 1, this.cols - 1, (this.rows *this.cols));
process.stdout.write("\n Anticlockwise \n");
this.anticlockwise(matrix, 0, 0, this.rows - 1, this.cols - 1, (this.rows *this.cols));
}
}
}
function main(args) {
//Define matrix element
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 obj = new MyMatrix(matrix);
obj.spiral_view(matrix);
}
main();
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
# Python 3 Program
# Print Half clockwise and Half anticlockwise spiral view of a matrix
class MyMatrix :
def __init__(self, matrix) :
# Get matrix size
self.rows = len(matrix)
self.cols = len(matrix[0])
def clockwise(self, matrix, s_row, s_col, e_row, e_col, k) :
# Left to right
i = s_col
while (i <= e_col and k > 0) :
print(" ", matrix[s_row][i], end = "")
i += 1
k -= 1
# Top to down
i = s_row + 1
while (i <= e_row and k > 0) :
print(" ", matrix[i][e_col], end = "")
i += 1
k -= 1
# Bottom right to bottom-left
i = e_col - 1
while (i >= s_col and k > 0) :
print(" ", matrix[e_row][i], end = "")
i -= 1
k -= 1
# Bottom left to top
i = e_row - 1
while (i > s_row and k > 0) :
print(" ", matrix[i][s_row], end = "")
i -= 1
k -= 1
if (s_row + 1 <= e_row - 1 and k > 0) :
self.clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
def anticlockwise(self, matrix, s_row, s_col, e_row, e_col, k) :
# Top to down
i = s_row
while (i <= e_row and k > 0) :
print(" ", matrix[i][s_col], end = "")
i += 1
k -= 1
# Left to right
i = s_col + 1
while (i <= e_col and k > 0) :
print(" ", matrix[e_row][i], end = "")
i += 1
k -= 1
# Bottom left to top
i = e_row - 1
while (i >= s_row and k > 0) :
print(" ", matrix[i][e_col], end = "")
i -= 1
k -= 1
# top right to left
i = e_col - 1
while (i > s_col and k > 0) :
print(" ", matrix[s_row][i], end = "")
i -= 1
k -= 1
if (s_row + 1 <= e_row - 1 and k > 0) :
self.anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
def spiral_view(self, matrix) :
if (self.cols > 2) :
# When exists more than 2 columns
if (self.cols % 2 == 0) :
print("\n Clockwise \n", end = "")
self.clockwise(matrix, 0, 0, self.rows - 1, (int(self.cols / 2)) - 1, int((self.rows * self.cols) / 2))
print("\n Anticlockwise \n", end = "")
self.anticlockwise(matrix, 0, (int(self.cols / 2)), self.rows - 1, self.cols - 1, int((self.rows * self.cols) / 2))
else :
print("\n Clockwise \n", end = "")
self.clockwise(matrix, 0, 0, self.rows - 1, (int(self.cols / 2)), int(self.rows * (self.cols + 1) / 2))
print("\n Anticlockwise \n", end = "")
self.anticlockwise(matrix, 0, (int(self.cols / 2)), self.rows - 1, self.cols - 1, int(self.rows * (self.cols + 1) / 2))
elif (self.cols == 2) :
print("\n Clockwise \n", end = "")
self.clockwise(matrix, 0, 0, self.rows - 1, self.cols - 1, (self.rows * self.cols))
print("\n Anticlockwise \n", end = "")
self.anticlockwise(matrix, 0, 0, self.rows - 1, self.cols - 1, (self.rows * self.cols))
def main() :
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]
]
obj = MyMatrix(matrix)
obj.spiral_view(matrix)
if __name__ == "__main__":
main()
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
# Ruby Program
# Print Half clockwise and Half anticlockwise spiral view of a matrix
class MyMatrix
# Define the accessor and reader of class MyMatrix
attr_reader :rows, :cols
attr_accessor :rows, :cols
def initialize(matrix)
# Get matrix size
self.rows = matrix.length
self.cols = matrix[0].length
end
def clockwise(matrix, s_row, s_col, e_row, e_col, k)
# Left to right
i = s_col
while (i <= e_col && k > 0)
print(" ", matrix[s_row][i])
i += 1
k -= 1
end
# Top to down
i = s_row + 1
while (i <= e_row && k > 0)
print(" ", matrix[i][e_col])
i += 1
k -= 1
end
# Bottom right to bottom-left
i = e_col - 1
while (i >= s_col && k > 0)
print(" ", matrix[e_row][i])
i -= 1
k -= 1
end
# Bottom left to top
i = e_row - 1
while (i > s_row && k > 0)
print(" ", matrix[i][s_row])
i -= 1
k -= 1
end
if (s_row + 1 <= e_row - 1 && k > 0)
self.clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
end
end
def anticlockwise(matrix, s_row, s_col, e_row, e_col, k)
# Top to down
i = s_row
while (i <= e_row && k > 0)
print(" ", matrix[i][s_col])
i += 1
k -= 1
end
# Left to right
i = s_col + 1
while (i <= e_col && k > 0)
print(" ", matrix[e_row][i])
i += 1
k -= 1
end
# Bottom left to top
i = e_row - 1
while (i >= s_row && k > 0)
print(" ", matrix[i][e_col])
i -= 1
k -= 1
end
# top right to left
i = e_col - 1
while (i > s_col && k > 0)
print(" ", matrix[s_row][i])
i -= 1
k -= 1
end
if (s_row + 1 <= e_row - 1 && k > 0)
self.anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k)
end
end
def spiral_view(matrix)
if (self.cols > 2)
# When exists more than 2 columns
if (self.cols % 2 == 0)
print("\n Clockwise \n")
self.clockwise(matrix, 0, 0, self.rows - 1, (self.cols / 2) - 1, (self.rows * self.cols) / 2)
print("\n Anticlockwise \n")
self.anticlockwise(matrix, 0, (self.cols / 2), self.rows - 1, self.cols - 1, (self.rows * self.cols) / 2)
else
print("\n Clockwise \n")
self.clockwise(matrix, 0, 0, self.rows - 1, (self.cols / 2), self.rows * (self.cols + 1) / 2)
print("\n Anticlockwise \n")
self.anticlockwise(matrix, 0, (self.cols / 2), self.rows - 1, self.cols - 1, self.rows * (self.cols + 1) / 2)
end
elsif (self.cols == 2)
print("\n Clockwise \n")
self.clockwise(matrix, 0, 0, self.rows - 1, self.cols - 1, (self.rows * self.cols))
print("\n Anticlockwise \n")
self.anticlockwise(matrix, 0, 0, self.rows - 1, self.cols - 1, (self.rows * self.cols))
end
end
end
def main()
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]
]
obj = MyMatrix.new(matrix)
obj.spiral_view(matrix)
end
main()
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
/*
Scala Program
Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
class MyMatrix(var rows: Int,var cols: Int) {
def this(matrix: Array[Array[Int]]) {
//Get matrix size
this( matrix.length,matrix(0).length);
}
def clockwise(matrix: Array[Array[Int]], s_row: Int, s_col: Int, e_row: Int, e_col: Int, k_size: Int): Unit = {
//Left to right
var i: Int = s_col;
var k: Int = k_size;
while (i <= e_col && k > 0) {
print(" " + matrix(s_row)(i));
i += 1;
k -= 1;
}
//Top to down
i = s_row + 1;
while (i <= e_row && k > 0) {
print(" " + matrix(i)(e_col));
i += 1;
k -= 1;
}
//Bottom right to bottom-left
i = e_col - 1;
while (i >= s_col && k > 0) {
print(" " + matrix(e_row)(i));
i -= 1;
k -= 1;
}
//Bottom left to top
i = e_row - 1;
while (i > s_row && k > 0) {
print(" " + matrix(i)(s_row));
i -= 1;
k -= 1;
}
if (s_row + 1 <= e_row - 1 && k > 0) {
this.clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
def anticlockwise(matrix: Array[Array[Int]], s_row: Int, s_col: Int, e_row: Int, e_col: Int, k_size: Int): Unit = {
//Top to down
var i: Int = s_row;
var k: Int = k_size;
while (i <= e_row && k > 0) {
print(" " + matrix(i)(s_col));
i += 1;
k -= 1;
}
//Left to right
i = s_col + 1;
while (i <= e_col && k > 0) {
print(" " + matrix(e_row)(i));
i += 1;
k -= 1;
}
//Bottom left to top
i = e_row - 1;
while (i >= s_row && k > 0) {
print(" " + matrix(i)(e_col));
i -= 1;
k -= 1;
}
//top right to left
i = e_col - 1;
while (i > s_col && k > 0) {
print(" " + matrix(s_row)(i));
i -= 1;
k -= 1;
}
if (s_row + 1 <= e_row - 1 && k > 0) {
this.anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
def spiral_view(matrix: Array[Array[Int]]): Unit = {
if (this.cols > 2) {
//When exists more than 2 columns
if (this.cols % 2 == 0) {
print("\n Clockwise \n");
this.clockwise(matrix, 0, 0, this.rows - 1, ((this.cols / 2).toInt) - 1, ((this.rows * this.cols) / 2).toInt);
print("\n Anticlockwise \n");
this.anticlockwise(matrix, 0, ((this.cols / 2).toInt), this.rows - 1, this.cols - 1, ((this.rows * this.cols) / 2).toInt);
} else {
print("\n Clockwise \n");
this.clockwise(matrix, 0, 0, this.rows - 1, ((this.cols / 2).toInt), (this.rows * (this.cols + 1) / 2).toInt);
print("\n Anticlockwise \n");
this.anticlockwise(matrix, 0, ((this.cols / 2).toInt), this.rows - 1, this.cols - 1, (this.rows * (this.cols + 1) / 2).toInt);
}
} else
if (this.cols == 2) {
print("\n Clockwise \n");
this.clockwise(matrix, 0, 0, this.rows - 1, this.cols - 1, (this.rows * this.cols));
print("\n Anticlockwise \n");
this.anticlockwise(matrix, 0, 0, this.rows - 1, this.cols - 1, (this.rows * this.cols));
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val 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));
val obj: MyMatrix = new MyMatrix(matrix);
obj.spiral_view(matrix);
}
}
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
/*
Swift Program
Print Half clockwise and Half anticlockwise spiral view of a matrix
*/
class MyMatrix {
var rows: Int;
var cols: Int;
init(_ matrix: [
[Int]
]) {
//Get matrix size
self.rows = matrix.count;
self.cols = matrix[0].count;
}
func clockwise(_ matrix: [
[Int]
], _ s_row: Int, _ s_col: Int, _ e_row: Int, _ e_col: Int, _ k_size: Int) {
//Left to right
var i: Int = s_col;
var k : Int = k_size;
while (i <= e_col && k > 0) {
print(" ", matrix[s_row][i], terminator: "");
i += 1;
k -= 1;
}
//Top to down
i = s_row + 1;
while (i <= e_row && k > 0) {
print(" ", matrix[i][e_col], terminator: "");
i += 1;
k -= 1;
}
//Bottom right to bottom-left
i = e_col - 1;
while (i >= s_col && k > 0) {
print(" ", matrix[e_row][i], terminator: "");
i -= 1;
k -= 1;
}
//Bottom left to top
i = e_row - 1;
while (i > s_row && k > 0) {
print(" ", matrix[i][s_row], terminator: "");
i -= 1;
k -= 1;
}
if (s_row + 1 <= e_row - 1 && k > 0) {
self.clockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
func anticlockwise(_ matrix: [
[Int]
], _ s_row: Int, _ s_col: Int, _ e_row: Int, _ e_col: Int, _ k_size: Int) {
//Top to down
var i: Int = s_row;
var k : Int = k_size;
while (i <= e_row && k > 0) {
print(" ", matrix[i][s_col], terminator: "");
i += 1;
k -= 1;
}
//Left to right
i = s_col + 1;
while (i <= e_col && k > 0) {
print(" ", matrix[e_row][i], terminator: "");
i += 1;
k -= 1;
}
//Bottom left to top
i = e_row - 1;
while (i >= s_row && k > 0) {
print(" ", matrix[i][e_col], terminator: "");
i -= 1;
k -= 1;
}
//top right to left
i = e_col - 1;
while (i > s_col && k > 0) {
print(" ", matrix[s_row][i], terminator: "");
i -= 1;
k -= 1;
}
if (s_row + 1 <= e_row - 1 && k > 0) {
self.anticlockwise(matrix, s_row + 1, s_col + 1, e_row - 1, e_col - 1, k);
}
}
func spiral_view(_ matrix: [
[Int]
]) {
if (self.cols > 2) {
//When exists more than 2 columns
if (self.cols % 2 == 0) {
print("\n Clockwise \n", terminator: "");
self.clockwise(matrix, 0, 0, self.rows - 1, (self.cols / 2) - 1, (self.rows * self.cols) / 2);
print("\n Anticlockwise \n", terminator: "");
self.anticlockwise(matrix, 0, (self.cols / 2), self.rows - 1, self.cols - 1, (self.rows * self.cols) / 2);
} else {
print("\n Clockwise \n", terminator: "");
self.clockwise(matrix, 0, 0, self.rows - 1, (self.cols / 2), self.rows * (self.cols + 1) / 2);
print("\n Anticlockwise \n", terminator: "");
self.anticlockwise(matrix, 0, (self.cols / 2), self.rows - 1, self.cols - 1, self.rows * (self.cols + 1) / 2);
}
} else
if (self.cols == 2) {
print("\n Clockwise \n", terminator: "");
self.clockwise(matrix, 0, 0, self.rows - 1, self.cols - 1, (self.rows * self.cols));
print("\n Anticlockwise \n", terminator: "");
self.anticlockwise(matrix, 0, 0, self.rows - 1, self.cols - 1, (self.rows * self.cols));
}
}
}
func main() {
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 obj: MyMatrix = MyMatrix(matrix);
obj.spiral_view(matrix);
}
main();
Output
Clockwise
1 2 3 24 37 42 41 32 15 16 17 18 19 20 21 22 23 36 35 34 33
Anticlockwise
4 25 38 39 40 31 14 13 12 11 10 9 8 7 6 5 26 27 28 29 30
Output Explanation
The mentioned Java code correctly implements the above algorithm to print the half-clockwise and half-anticlockwise spiral views of the matrix. It identifies the boundaries of the matrix and divides it into half-clockwise and half-anticlockwise regions. It then recursively prints the spiral views of these regions. The output matches the expected half-clockwise and half-anticlockwise spiral views of the given matrix.
Time Complexity
The time complexity of the mentioned solution is O(N), where N is the number of elements in the matrix. The
clockwise
and anticlockwise
functions iterate through each element of the matrix exactly
once, printing the elements in the desired order. The spiral_view
function performs constant time
operations based on the number of columns in the matrix. Therefore, the overall time complexity is O(N).
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