Reversal spiral of diamond element in square matrix

Reversal spiral of diamond element

Here given code implementation process.

/*
  C Program 
+ Reversal spiral of diamond element in square matrix
*/
#include <stdio.h>
#define SIZE 7
#define SIZE 7
void spiralView(int matrix[SIZE][SIZE], int result[], 
  int s_row, int s_col, int e_row, int e_col, int k)
{
	int mid_col = s_col + ((e_col - s_col) / 2);
	int mid_row = mid_col;
	for (int i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++)
	{
		result[k - 1] = matrix[s_row + j][i];
	}
	for (int i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++)
	{
		result[k - 1] = matrix[(mid_row) + j][i];
	}
	for (int i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++)
	{
		result[k - 1] = matrix[(e_col) - j][i];
	}
	for (int i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++)
	{
		result[k - 1] = matrix[(mid_row) - j][i];
	}
	if (s_row + 1 <= e_row - 1 && k > 0)
	{
		// Recursive call
		spiralView(matrix, result, s_row + 1, s_col + 1, 
                   e_row - 1, e_col - 1, k);
	}
}
// Returns the number of elements in diamond shape
int elementSize(int col)
{
	int counter = 0;
	while (col > 0)
	{
		counter += col;
		col--;
	}
	return counter *4;
}
void printElement(int arr[SIZE][SIZE], int row, int col)
{
	for (int i = col; i < (SIZE) - col; ++i)
	{
		printf("%3d", arr[row][i]);
	}
}
// Include space
void space(int size)
{
	for (int i = 0; i <= size; ++i)
	{
		printf(" ");
	}
}
void diamondView(int arr[SIZE][SIZE])
{
	if (SIZE != SIZE || SIZE % 2 == 0)
	{
		printf("\n Not a valid perfect Odd square matrix");
		return;
	}
	int col = SIZE / 2;
	int counter = col *3;
	int distance = 3;
	for (int i = 0; i < SIZE; ++i)
	{
		space(counter);
		printElement(arr, i, col);
		if (i < SIZE / 2)
		{
			counter -= distance;
			col--;
		}
		else
		{
			col++;
			counter += distance;
		}
		printf("\n");
	}
}
void diamondSpiral(int matrix[SIZE][SIZE])
{
	if (SIZE % 2 == 0)
	{
		printf("\n Not a valid perfect Odd square matrix");
		return;
	}
	// Get number of elements in diamond shap
	int size = elementSize((SIZE) / 2) + 1;
	if (size == 0)
	{
		return;
	}
	// This is used to collect result
	int result[size];
	// Find resultant element
	spiralView(matrix, result, 0, 0, SIZE - 1, SIZE - 1, size);
	// Display calculated result
	for (int i = 0; i < size; ++i)
	{
		printf("%4d", result[i]);
	}
}
int main()
{
	// Matrix of integer elements
    int matrix[SIZE][SIZE] = {
        {1,  2,   3,  4,  5   ,6, 7},
        {8,  9,  10,  11, 12, 13, 14},  
        {15, 16, 17, 18,  19, 20, 21},
        {22, 23, 24, 25,  26, 27, 28},
        {29, 30, 31, 32,  33, 34, 35},
        {36, 37, 38, 39,  40, 41, 42},
        {43, 44, 45, 46,  47, 48, 49}
    };
	// Display diamond view
	diamondView(matrix);
	printf("\n");
	// Display diamond spiral view
	diamondSpiral(matrix);
	return 0;
}

Output

            4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46

  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12   4
// Java program
// Reversal spiral of diamond element in square matrix
public class Spiral
{
	public void show(int[][] arr, int row, int col)
	{
		int size = arr.length;
		for (int i = col; i < (size) - col; ++i)
		{
			System.out.print(" " + arr[row][i]);
		}
	}
	public void space(int size)
	{
		for (int i = 0; i <= size; ++i)
		{
			System.out.print(" ");
		}
	}
	public void diamond_view(int[][] arr, int size)
	{
		if (size % 2 == 0)
		{
			System.out.println("\nNot a valid perfect Odd square matrix");
			return;
		}
		int col = size / 2;
		int counter = col * 3;
		int distance = 3;
		for (int i = 0; i < size; ++i)
		{
			space(counter);
			show(arr, i, col);
			if (i < size / 2)
			{
				counter -= distance;
				col--;
			}
			else
			{
				col++;
				counter += distance;
			}
			System.out.println();
		}
	}
	public void spiral_view(int[][] matrix, 
                            int[] result, int s_row, 
      int s_col, int e_row, int e_col, int k)
	{
		int mid_col = s_col + ((e_col - s_col) / 2);
		int mid_row = mid_col;
		for (int i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[s_row + j][i];
		}
		for (int i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) + j][i];
		}
		for (int i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(e_col) - j][i];
		}
		for (int i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) - j][i];
		}
		if (s_row + 1 <= e_row - 1 && k > 0)
		{
			//Recursive call
			spiral_view(matrix, result, s_row + 1, 
                        s_col + 1, e_row - 1, e_col - 1, k);
		}
	}
	public int element_size(int col)
	{
		int counter = 0;
		while (col > 0)
		{
			counter += col;
			col--;
		}
		return counter * 4;
	}
	public void diamond_spiral(int[][] matrix, int row)
	{
		if (row % 2 == 0)
		{
			System.out.print("\nNot a valid perfect Odd square matrix");
			return;
		}
		int size = element_size((row) / 2) + 1;
		int[] result = new int[size];
		spiral_view(matrix, result, 0, 0, row - 1, row - 1, size);
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + result[i]);
		}
	}
	public static void main(String[] args)
	{
		Spiral task = new Spiral();
	    // Matrix of integer elements
        int [][]matrix = {
            {1,  2,   3,  4,  5   ,6, 7},
            {8,  9,  10,  11, 12, 13, 14},  
            {15, 16, 17, 18,  19, 20, 21},
            {22, 23, 24, 25,  26, 27, 28},
            {29, 30, 31, 32,  33, 34, 35},
            {36, 37, 38, 39,  40, 41, 42},
            {43, 44, 45, 46,  47, 48, 49}
        };
		// Get the length of Odd square matrix
		int size = matrix.length;
		// Display diamond view
		task.diamond_view(matrix, size);
		// Display diamond spiral view
		task.diamond_spiral(matrix, size);
	}
}

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4
//C++ Program 
//Reversal spiral of diamond element in square matrix
#include <iostream>
#define SIZE 7
using namespace std;

class MyArray
{
	public: void show(int arr[SIZE][SIZE], int row, int col)
	{
		for (int i = col; i < (SIZE) - col; ++i)
		{
			cout << " " << arr[row][i];
		}
	}
	void space(int size)
	{
		for (int i = 0; i <= size; ++i)
		{
			cout << (" ");
		}
	}
	void diamond_view(int arr[SIZE][SIZE])
	{
		if (SIZE != SIZE || SIZE % 2 == 0)
		{
			cout << ("\nNot a valid perfect Odd square matrix");
			return;
		}
		int col = SIZE / 2;
		int counter = col *3;
		int distance = 3;
		for (int i = 0; i < SIZE; ++i)
		{
			space(counter);
			show(arr, i, col);
			if (i < SIZE / 2)
			{
				counter -= distance;
				col--;
			}
			else
			{
				col++;
				counter += distance;
			}
			cout << ("\n");
		}
		cout << ("\n");
	}
	void spiral_view(int matrix[SIZE][SIZE], 
      int result[], int s_row, int s_col, int e_row, int e_col, int k)
	{
		int mid_col = s_col + ((e_col - s_col) / 2);
		int mid_row = mid_col;
		for (int i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[s_row + j][i];
		}
		for (int i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) + j][i];
		}
		for (int i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(e_col) - j][i];
		}
		for (int i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) - j][i];
		}
		if (s_row + 1 <= e_row - 1 && k > 0)
		{
			//Recursive call
			spiral_view(matrix, result, s_row + 1, 
                        s_col + 1, e_row - 1, e_col - 1, k);
		}
	}
	int element_size(int col)
	{
		int counter = 0;
		while (col > 0)
		{
			counter += col;
			col--;
		}
		return counter *4;
	}
	void diamond_spiral(int matrix[SIZE][SIZE])
	{
		if (SIZE % 2 == 0)
		{
			cout << ("\nNot a valid perfect Odd square matrix");
			return;
		}
		int size = element_size((SIZE) / 2) + 1;
		int result[size];
		spiral_view(matrix, result, 0, 0, SIZE - 1, SIZE - 1, size);
		for (int i = 0; i < size; ++i)
		{
			cout << "  " << result[i];
		}
	}
};
int main()
{
	MyArray obj;
    int matrix[SIZE][SIZE] = {
      {1,  2,   3,  4,  5   ,6, 7},
      {8,  9,  10,  11, 12, 13, 14},  
      {15, 16, 17, 18,  19, 20, 21},
      {22, 23, 24, 25,  26, 27, 28},
      {29, 30, 31, 32,  33, 34, 35},
      {36, 37, 38, 39,  40, 41, 42},
      {43, 44, 45, 46,  47, 48, 49}
    };
	obj.diamond_view(matrix);
	obj.diamond_spiral(matrix);
	return 0;
}

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46

  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4
// Include namespace system
using System;
// Csharp program
// Reversal spiral of diamond element in square matrix
public class Spiral
{
	public void show(int[,] arr, int row, int col)
	{
		int size = arr.GetLength(0);
		for (int i = col; i < (size) - col; ++i)
		{
			Console.Write(" " + arr[row,i]);
		}
	}
	public void space(int size)
	{
		for (int i = 0; i <= size; ++i)
		{
			Console.Write(" ");
		}
	}
	public void diamond_view(int[,] arr, int size)
	{
		if (size % 2 == 0)
		{
			Console.WriteLine("\nNot a valid perfect Odd square matrix");
			return;
		}
		int col = size / 2;
		int counter = col * 3;
		int distance = 3;
		for (int i = 0; i < size; ++i)
		{
			this.space(counter);
			this.show(arr, i, col);
			if (i < size / 2)
			{
				counter -= distance;
				col--;
			}
			else
			{
				col++;
				counter += distance;
			}
			Console.WriteLine();
		}
	}
	public void spiral_view(int[,] matrix, 
                            int[] result, int s_row, 
      int s_col, int e_row, int e_col, int k)
	{
		int mid_col = s_col + ((e_col - s_col) / 2);
		int mid_row = mid_col;
		for (int i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[s_row + j,i];
		}
		for (int i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) + j,i];
		}
		for (int i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(e_col) - j,i];
		}
		for (int i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) - j,i];
		}
		if (s_row + 1 <= e_row - 1 && k > 0)
		{
			//Recursive call
			this.spiral_view(matrix, result, 
                             s_row + 1, s_col + 1, 
                             e_row - 1, e_col - 1, k);
		}
	}
	public int element_size(int col)
	{
		int counter = 0;
		while (col > 0)
		{
			counter += col;
			col--;
		}
		return counter * 4;
	}
	public void diamond_spiral(int[,] matrix, int row)
	{
		if (row % 2 == 0)
		{
			Console.Write("\nNot a valid perfect Odd square matrix");
			return;
		}
		int size = this.element_size((row) / 2) + 1;
		int[] result = new int[size];
		this.spiral_view(matrix, result, 0, 0, row - 1, row - 1, size);
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + result[i]);
		}
	}
	public static void Main(String[] args)
	{
		Spiral task = new Spiral();
		// Matrix of integer elements
		int[,] matrix = {
			{
				1 , 2 , 3 , 4 , 5 , 6 , 7
			},
			{
				8 , 9 , 10 , 11 , 12 , 13 , 14
			},
			{
				15 , 16 , 17 , 18 , 19 , 20 , 21
			},
			{
				22 , 23 , 24 , 25 , 26 , 27 , 28
			},
			{
				29 , 30 , 31 , 32 , 33 , 34 , 35
			},
			{
				36 , 37 , 38 , 39 , 40 , 41 , 42
			},
			{
				43 , 44 , 45 , 46 , 47 , 48 , 49
			}
		};
		// Get the length of Odd square matrix
		int size = matrix.GetLength(0);
		// Display diamond view
		task.diamond_view(matrix, size);
		// Display diamond spiral view
		task.diamond_spiral(matrix, size);
	}
}

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4
<?php
// Php program
// Reversal spiral of diamond element in square matrix
class Spiral
{
	public	function show($arr, $row, $col)
	{
		$size = count($arr);
		for ($i = $col; $i < ($size) - $col; ++$i)
		{
			echo(" ".$arr[$row][$i]);
		}
	}
	public function space($size)
	{
		for ($i = 0; $i <= $size; ++$i)
		{
			echo(" ");
		}
	}
	public	function diamond_view($arr, $size)
	{
		if ($size % 2 == 0)
		{
			echo("\nNot a valid perfect Odd square matrix\n");
			return;
		}
		$col = (int)($size / 2);
		$counter = $col * 3;
		$distance = 3;
		for ($i = 0; $i < $size; ++$i)
		{
			$this->space($counter);
			$this->show($arr, $i, $col);
			if ($i < (int)($size / 2))
			{
				$counter -= $distance;
				$col--;
			}
			else
			{
				$col++;
				$counter += $distance;
			}
			echo("\n");
		}
	}
	public	function spiral_view($matrix, &$result, 
                                  $s_row, $s_col, 
                                  $e_row, $e_col, $k)
	{
		$mid_col = $s_col + ((int)(($e_col - $s_col) / 2));
		$mid_row = $mid_col;
		for ($i = $mid_col, $j = 0; $i < $e_col && 
             $k > 0; ++$i, $k--, $j++)
		{
			$result[$k - 1] = $matrix[$s_row + $j][$i];
		}
		for ($i = $e_col, $j = 0; $i >= $mid_col && 
             $k > 0; --$i, $k--, $j++)
		{
			$result[$k - 1] = $matrix[($mid_row) + $j][$i];
		}
		for ($i = $mid_col - 1, $j = 1; $i >= $s_col && 
             $k > 0; --$i, $k--, $j++)
		{
			$result[$k - 1] = $matrix[($e_col) - $j][$i];
		}
		for ($i = $s_col + 1, $j = 1; $i < $mid_col && 
             $k > 0; ++$i, $k--, $j++)
		{
			$result[$k - 1] = $matrix[($mid_row) - $j][$i];
		}
		if ($s_row + 1 <= $e_row - 1 && $k > 0)
		{
			//Recursive call
			$this->spiral_view($matrix, $result,
                               $s_row + 1, $s_col + 1, 
                               $e_row - 1, $e_col - 1, $k);
		}
	}
	public	function element_size($col)
	{
		$counter = 0;
		while ($col > 0)
		{
			$counter += $col;
			$col--;
		}
		return $counter * 4;
	}
	public	function diamond_spiral($matrix, $row)
	{
		if ($row % 2 == 0)
		{
			echo("\nNot a valid perfect Odd square matrix");
			return;
		}
		$size = $this->element_size((int)(($row) / 2)) + 1;
		$result = array_fill(0, $size, 0);
		$this->spiral_view($matrix, $result, 0, 0, 
                           $row - 1, $row - 1, $size);
		for ($i = 0; $i < $size; ++$i)
		{
			echo("  ".$result[$i]);
		}
	}
}

function main()
{
	$task = new Spiral();
	// Matrix of integer elements
	$matrix = array(
      array(1, 2, 3, 4, 5, 6, 7), 
      array(8, 9, 10, 11, 12, 13, 14), 
      array(15, 16, 17, 18, 19, 20, 21), 
      array(22, 23, 24, 25, 26, 27, 28),
      array(29, 30, 31, 32, 33, 34, 35),
      array(36, 37, 38, 39, 40, 41, 42), 
      array(43, 44, 45, 46, 47, 48, 49)
   );
	// Get the length of Odd square matrix
	$size = count($matrix);
	// Display diamond view
	$task->diamond_view($matrix, $size);
	// Display diamond spiral view
	$task->diamond_spiral($matrix, $size);
}
main();

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4
// Node JS program
// Reversal spiral of diamond element in square matrix
class Spiral
{
	show(arr, row, col)
	{
		var size = arr.length;
		for (var i = col; i < (size) - col; ++i)
		{
			process.stdout.write(" " + arr[row][i]);
		}
	}
	space(size)
	{
		for (var i = 0; i <= size; ++i)
		{
			process.stdout.write(" ");
		}
	}
	diamond_view(arr, size)
	{
		if (size % 2 == 0)
		{
			console.log("\nNot a valid perfect Odd square matrix");
			return;
		}
		var col = parseInt(size / 2);
		var counter = col * 3;
		var distance = 3;
		for (var i = 0; i < size; ++i)
		{
			this.space(counter);
			this.show(arr, i, col);
			if (i < parseInt(size / 2))
			{
				counter -= distance;
				col--;
			}
			else
			{
				col++;
				counter += distance;
			}
			console.log();
		}
	}
	spiral_view(matrix, result, s_row, s_col, e_row, e_col, k)
	{
		var mid_col = s_col + (parseInt((e_col - s_col) / 2));
		var mid_row = mid_col;
		for (var i = mid_col, j = 0; i < e_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[s_row + j][i];
		}
		for (var i = e_col, j = 0; i >= mid_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) + j][i];
		}
		for (var i = mid_col - 1, j = 1; i >= s_col && k > 0; --i, k--, j++)
		{
			result[k - 1] = matrix[(e_col) - j][i];
		}
		for (var i = s_col + 1, j = 1; i < mid_col && k > 0; ++i, k--, j++)
		{
			result[k - 1] = matrix[(mid_row) - j][i];
		}
		if (s_row + 1 <= e_row - 1 && k > 0)
		{
			//Recursive call
			this.spiral_view(matrix, result, 
                             s_row + 1, s_col + 1, 
                             e_row - 1, e_col - 1, k);
		}
	}
	element_size(col)
	{
		var counter = 0;
		while (col > 0)
		{
			counter += col;
			col--;
		}
		return counter * 4;
	}
	diamond_spiral(matrix, row)
	{
		if (row % 2 == 0)
		{
			process.stdout.write("\nNot a valid perfect Odd square matrix");
			return;
		}
		var size = this.element_size(parseInt((row) / 2)) + 1;
		var result = Array(size).fill(0);
		this.spiral_view(matrix, result, 0, 0, row - 1, row - 1, size);
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + result[i]);
		}
	}
}

function main()
{
	var task = new Spiral();
	// Matrix of integer elements
	var matrix = [
		[1, 2, 3, 4, 5, 6, 7],
		[8, 9, 10, 11, 12, 13, 14],
		[15, 16, 17, 18, 19, 20, 21],
		[22, 23, 24, 25, 26, 27, 28],
		[29, 30, 31, 32, 33, 34, 35],
		[36, 37, 38, 39, 40, 41, 42],
		[43, 44, 45, 46, 47, 48, 49]
	];
	// Get the length of Odd square matrix
	var size = matrix.length;
	// Display diamond view
	task.diamond_view(matrix, size);
	// Display diamond spiral view
	task.diamond_spiral(matrix, size);
}
main();

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4
#  Python 3 program
#  Reversal spiral of diamond element in square matrix
class Spiral :
	def show(self, arr, row, col) :
		size = len(arr)
		i = col
		while (i < (size) - col) :
			print("", arr[row][i], end = "")
			i += 1
		
	
	def space(self, size) :
		i = 0
		while (i <= size) :
			print(end = " ")
			i += 1
		
	
	def diamond_view(self, arr, size) :
		if (size % 2 == 0) :
			print("\nNot a valid perfect Odd square matrix")
			return
		
		col = int(size / 2)
		counter = col * 3
		distance = 3
		i = 0
		while (i < size) :
			self.space(counter)
			self.show(arr, i, col)
			if (i < int(size / 2)) :
				counter -= distance
				col -= 1
			else :
				col += 1
				counter += distance
			
			print()
			i += 1
		
	
	def spiral_view(self, matrix, result, s_row, s_col, e_row, e_col, k) :
		mid_col = s_col + (int((e_col - s_col) / 2))
		mid_row = mid_col
		i = mid_col
		j = 0
		while (i < e_col and k > 0) :
			result[k - 1] = matrix[s_row + j][i]
			i += 1
			k -= 1
			j += 1
		
		i = e_col
		j = 0
		while (i >= mid_col and k > 0) :
			result[k - 1] = matrix[(mid_row) + j][i]
			i -= 1
			k -= 1
			j += 1
		
		i = mid_col - 1
		j = 1
		while (i >= s_col and k > 0) :
			result[k - 1] = matrix[(e_col) - j][i]
			i -= 1
			k -= 1
			j += 1
		
		i = s_col + 1
		j = 1
		while (i < mid_col and k > 0) :
			result[k - 1] = matrix[(mid_row) - j][i]
			i += 1
			k -= 1
			j += 1
		
		if (s_row + 1 <= e_row - 1 and k > 0) :
			# Recursive call
			self.spiral_view(matrix, result, s_row + 1, 
                             s_col + 1, e_row - 1, e_col - 1, k)
		
	
	def element_size(self, col) :
		counter = 0
		while (col > 0) :
			counter += col
			col -= 1
		
		return counter * 4
	
	def diamond_spiral(self, matrix, row) :
		if (row % 2 == 0) :
			print("\nNot a valid perfect Odd square matrix", end = "")
			return
		
		size = self.element_size(int((row) / 2)) + 1
		result = [0] * (size)
		self.spiral_view(matrix, result, 0, 0, row - 1, row - 1, size)
		i = 0
		while (i < size) :
			print("", result[i], end = "")
			i += 1
		
	

def main() :
	task = Spiral()
	#  Matrix of integer elements
	matrix = [
		[1, 2, 3, 4, 5, 6, 7],
		[8, 9, 10, 11, 12, 13, 14],
		[15, 16, 17, 18, 19, 20, 21],
		[22, 23, 24, 25, 26, 27, 28],
		[29, 30, 31, 32, 33, 34, 35],
		[36, 37, 38, 39, 40, 41, 42],
		[43, 44, 45, 46, 47, 48, 49]
	]
	#  Get the length of Odd square matrix
	size = len(matrix)
	#  Display diamond view
	task.diamond_view(matrix, size)
	#  Display diamond spiral view
	task.diamond_spiral(matrix, size)

if __name__ == "__main__": main()

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
 25 24 32 26 18 17 23 31 39 33 27 19 11 10 16 22 30 38 46 40 34 28 20 12 4
#  Ruby program
#  Reversal spiral of diamond element in square matrix
class Spiral 
	def show(arr, row, col) 
		size = arr.length
		i = col
		while (i < (size) - col) 
			print(" ", arr[row][i])
			i += 1
		end

	end

	def space(size) 
		i = 0
		while (i <= size) 
			print(" ")
			i += 1
		end

	end

	def diamond_view(arr, size) 
		if (size % 2 == 0) 
			print("\nNot a valid perfect Odd square matrix", "\n")
			return
		end

		col = size / 2
		counter = col * 3
		distance = 3
		i = 0
		while (i < size) 
			self.space(counter)
			self.show(arr, i, col)
			if (i < size / 2) 
				counter -= distance
				col -= 1
			else
 
				col += 1
				counter += distance
			end

			print("\n")
			i += 1
		end

	end

	def spiral_view(matrix, result, s_row, 
                    s_col, e_row, e_col, k) 
		mid_col = s_col + ((e_col - s_col) / 2)
		mid_row = mid_col
		i = mid_col
		j = 0
		while (i < e_col && k > 0) 
			result[k - 1] = matrix[s_row + j][i]
			i += 1
			k -= 1
			j += 1
		end

		i = e_col
		j = 0
		while (i >= mid_col && k > 0) 
			result[k - 1] = matrix[(mid_row) + j][i]
			i -= 1
			k -= 1
			j += 1
		end

		i = mid_col - 1
		j = 1
		while (i >= s_col && k > 0) 
			result[k - 1] = matrix[(e_col) - j][i]
			i -= 1
			k -= 1
			j += 1
		end

		i = s_col + 1
		j = 1
		while (i < mid_col && k > 0) 
			result[k - 1] = matrix[(mid_row) - j][i]
			i += 1
			k -= 1
			j += 1
		end

		if (s_row + 1 <= e_row - 1 && k > 0) 
			# Recursive call
			self.spiral_view(matrix, result, 
                             s_row + 1, s_col + 1, 
                             e_row - 1, e_col - 1, k)
		end

	end

	def element_size(col) 
		counter = 0
		while (col > 0) 
			counter += col
			col -= 1
		end

		return counter * 4
	end

	def diamond_spiral(matrix, row) 
		if (row % 2 == 0) 
			print("\nNot a valid perfect Odd square matrix")
			return
		end

		size = self.element_size((row) / 2) + 1
		result = Array.new(size) {0}
		self.spiral_view(matrix, result, 0, 0, 
                         row - 1, row - 1, size)
		i = 0
		while (i < size) 
			print("  ", result[i])
			i += 1
		end

	end

end

def main() 
	task = Spiral.new()
	#  Matrix of integer elements
	matrix = [
		[1, 2, 3, 4, 5, 6, 7],
		[8, 9, 10, 11, 12, 13, 14],
		[15, 16, 17, 18, 19, 20, 21],
		[22, 23, 24, 25, 26, 27, 28],
		[29, 30, 31, 32, 33, 34, 35],
		[36, 37, 38, 39, 40, 41, 42],
		[43, 44, 45, 46, 47, 48, 49]
	]
	#  Get the length of Odd square matrix
	size = matrix.length
	#  Display diamond view
	task.diamond_view(matrix, size)
	#  Display diamond spiral view
	task.diamond_spiral(matrix, size)
end

main()

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4
// Scala program
// Reversal spiral of diamond element in square matrix
class Spiral()
{
	def show(arr: Array[Array[Int]], row: Int, col: Int): Unit = {
		var size: Int = arr.length;
		var i: Int = col;
		while (i < (size) - col)
		{
			print(" " + arr(row)(i));
			i += 1;
		}
	}
	def space(size: Int): Unit = {
		var i: Int = 0;
		while (i <= size)
		{
			print(" ");
			i += 1;
		}
	}
	def diamond_view(arr: Array[Array[Int]], size: Int): Unit = {
		if (size % 2 == 0)
		{
			println("\nNot a valid perfect Odd square matrix");
			return;
		}
		var col: Int = size / 2;
		var counter: Int = col * 3;
		var distance: Int = 3;
		var i: Int = 0;
		while (i < size)
		{
			space(counter);
			show(arr, i, col);
			if (i < size / 2)
			{
				counter -= distance;
				col -= 1;
			}
			else
			{
				col += 1;
				counter += distance;
			}
			println();
			i += 1;
		}
	}
	def spiral_view(matrix: Array[Array[Int]], 
      result: Array[Int], s_row: Int, s_col: Int, 
        e_row: Int, e_col: Int, k1: Int): Unit = {
		var mid_col: Int = s_col + ((e_col - s_col) / 2);
		var mid_row: Int = mid_col;
		var i: Int = mid_col;
        var j = 0;
		var k = k1;
		while (i < e_col && k > 0)
		{
			result(k - 1) = matrix(s_row + j)(i);
			i += 1;
			k -= 1;
			j += 1;
		}
		i = e_col;
        j = 0;
		while (i >= mid_col && k > 0)
		{
			result(k - 1) = matrix((mid_row) + j)(i);
			i -= 1;
			k -= 1;
			j += 1;
		}
		i = mid_col - 1;
		j = 1;
		while (i >= s_col && k > 0)
		{
			result(k - 1) = matrix((e_col) - j)(i);
			i -= 1;
			k -= 1;
			j += 1;
		}
		i = s_col + 1;
		j = 1;
		while (i < mid_col && k > 0)
		{
			result(k - 1) = matrix((mid_row) - j)(i);
			i += 1;
			k -= 1;
			j += 1;
		}
		if (s_row + 1 <= e_row - 1 && k > 0)
		{
			//Recursive call
			spiral_view(matrix, result, s_row + 1, 
                        s_col + 1, e_row - 1, e_col - 1, k);
		}
	}
	def element_size(c: Int): Int = {
		var counter: Int = 0;
      	var col = c;
		while (col > 0)
		{
			counter += col;
			col -= 1;
		}
		return counter * 4;
	}
	def diamond_spiral(matrix: Array[Array[Int]], row: Int): Unit = {
		if (row % 2 == 0)
		{
			print("\nNot a valid perfect Odd square matrix");
			return;
		}
		var size: Int = element_size((row) / 2) + 1;
		var result: Array[Int] = Array.fill[Int](size)(0);
		spiral_view(matrix, result, 0, 0, row - 1, row - 1, size);
		var i: Int = 0;
		while (i < size)
		{
			print("  " + result(i));
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Spiral = new Spiral();
		// Matrix of integer elements
		var matrix: Array[Array[Int]] = Array(
          Array(1, 2, 3, 4, 5, 6, 7), 
          Array(8, 9, 10, 11, 12, 13, 14), 
          Array(15, 16, 17, 18, 19, 20, 21), 
          Array(22, 23, 24, 25, 26, 27, 28), 
          Array(29, 30, 31, 32, 33, 34, 35), 
          Array(36, 37, 38, 39, 40, 41, 42), 
          Array(43, 44, 45, 46, 47, 48, 49)
        );
		// Get the length of Odd square matrix
		var size: Int = matrix.length;
		// Display diamond view
		task.diamond_view(matrix, size);
		// Display diamond spiral view
		task.diamond_spiral(matrix, size);
	}
}

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4
import Foundation;
// Swift 4 program
// Reversal spiral of diamond element in square matrix
class Spiral
{
	func show(_ arr: [[Int]], _ row: Int, _ col: Int)
	{
		let size: Int = arr.count;
		var i: Int = col;
		while (i < (size) - col)
		{
			print("", arr[row][i], terminator: "");
			i += 1;
		}
	}
	func space(_ size: Int)
	{
		var i: Int = 0;
		while (i <= size)
		{
			print(" ", terminator: "");
			i += 1;
		}
	}
	func diamond_view(_ arr: [[Int]], _ size: Int)
	{
		if (size % 2 == 0)
		{
			print("\nNot a valid perfect Odd square matrix");
			return;
		}
		var col: Int = size / 2;
		var counter: Int = col * 3;
		let distance: Int = 3;
		var i: Int = 0;
		while (i < size)
		{
			self.space(counter);
			self.show(arr, i, col);
			if (i < size / 2)
			{
				counter -= distance;
				col -= 1;
			}
			else
			{
				col += 1;
				counter += distance;
			}
			print();
			i += 1;
		}
	}
	func spiral_view(_ matrix: [[Int]], _ result: inout[Int], 
      _ s_row: Int, _ s_col: Int, _ e_row: Int, 
        _ e_col: Int, _ k1: Int)
	{
		let mid_col: Int = s_col + ((e_col - s_col) / 2);
		let mid_row: Int = mid_col;
		var i: Int = mid_col;
		var j = 0;
      	var k = k1;
		while (i < e_col && k > 0)
		{
			result[k - 1] = matrix[s_row + j][i];
			i += 1;
			k -= 1;
			j += 1;
		}
		i = e_col;
		j = 0;
		while (i >= mid_col && k > 0)
		{
			result[k - 1] = matrix[(mid_row) + j][i];
			i -= 1;
			k -= 1;
			j += 1;
		}
		i = mid_col - 1;
		j = 1;
		while (i >= s_col && k > 0)
		{
			result[k - 1] = matrix[(e_col) - j][i];
			i -= 1;
			k -= 1;
			j += 1;
		}
		i = s_col + 1;
		j = 1;
		while (i < mid_col && k > 0)
		{
			result[k - 1] = matrix[(mid_row) - j][i];
			i += 1;
			k -= 1;
			j += 1;
		}
		if (s_row + 1 <= e_row - 1 && k > 0)
		{
			//Recursive call
			self.spiral_view(matrix, &result, 
                             s_row + 1, s_col + 1, 
                             e_row - 1, e_col - 1, k);
		}
	}
	func element_size(_ c: Int) -> Int
	{
		var counter: Int = 0;
      	var col = c;
		while (col > 0)
		{
			counter += col;
			col -= 1;
		}
		return counter * 4;
	}
	func diamond_spiral(_ matrix: [
		[Int]
	], _ row: Int)
	{
		if (row % 2 == 0)
		{
			print("\nNot a valid perfect Odd square matrix", terminator: "");
			return;
		}
		let size: Int = self.element_size((row) / 2) + 1;
		var result: [Int] = Array(repeating: 0, count: size);
		self.spiral_view(matrix, &result, 0, 0, row - 1, row - 1, size);
		var i: Int = 0;
		while (i < size)
		{
			print("  ", result[i], terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let task: Spiral = Spiral();
	// Matrix of integer elements
	let matrix: [
		[Int]
	] = [
		[1, 2, 3, 4, 5, 6, 7],
		[8, 9, 10, 11, 12, 13, 14],
		[15, 16, 17, 18, 19, 20, 21],
		[22, 23, 24, 25, 26, 27, 28],
		[29, 30, 31, 32, 33, 34, 35],
		[36, 37, 38, 39, 40, 41, 42],
		[43, 44, 45, 46, 47, 48, 49]
	];
	// Get the length of Odd square matrix
	let size: Int = matrix.count;
	// Display diamond view
	task.diamond_view(matrix, size);
	// Display diamond spiral view
	task.diamond_spiral(matrix, size);
}
main();

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
   25   24   32   26   18   17   23   31   39   33   27   19   11   10   16   22   30   38   46   40   34   28   20   12   4
// Kotlin program
// Reversal spiral of diamond element in square matrix
class Spiral
{
	fun show(arr: Array < Array < Int >> , row: Int, col: Int): Unit
	{
		val size: Int = arr.count();
		var i: Int = col;
		while (i < (size) - col)
		{
			print(" " + arr[row][i]);
			i += 1;
		}
	}
	fun space(size: Int): Unit
	{
		var i: Int = 0;
		while (i <= size)
		{
			print(" ");
			i += 1;
		}
	}
	fun diamond_view(arr: Array < Array < Int >> , size: Int): Unit
	{
		if (size % 2 == 0)
		{
			println("\nNot a valid perfect Odd square matrix");
			return;
		}
		var col: Int = size / 2;
		var counter: Int = col * 3;
		val distance: Int = 3;
		var i: Int = 0;
		while (i < size)
		{
			this.space(counter);
			this.show(arr, i, col);
			if (i < size / 2)
			{
				counter -= distance;
				col -= 1;
			}
			else
			{
				col += 1;
				counter += distance;
			}
			println();
			i += 1;
		}
	}
	fun spiral_view(matrix: Array < Array < Int >> , 
                    result: Array < Int > , 
                    s_row: Int, s_col: Int, 
                    e_row: Int, e_col: Int, k1: Int): Unit
	{
		val mid_col: Int = s_col + ((e_col - s_col) / 2);
		val mid_row: Int = mid_col;
		var i: Int = mid_col;
		var j = 0;
      	var k = k1;
		while (i < e_col && k > 0)
		{
			result[k - 1] = matrix[s_row + j][i];
			i += 1;
			k -= 1;
			j += 1;
		}
		i = e_col;
		j = 0;
		while (i >= mid_col && k > 0)
		{
			result[k - 1] = matrix[(mid_row) + j][i];
			i -= 1;
			k -= 1;
			j += 1;
		}
		i = mid_col - 1;
		j = 1;
		while (i >= s_col && k > 0)
		{
			result[k - 1] = matrix[(e_col) - j][i];
			i -= 1;
			k -= 1;
			j += 1;
		}
		i = s_col + 1;
		j = 1;
		while (i < mid_col && k > 0)
		{
			result[k - 1] = matrix[(mid_row) - j][i];
			i += 1;
			k -= 1;
			j += 1;
		}
		if (s_row + 1 <= e_row - 1 && k > 0)
		{
			//Recursive call
			this.spiral_view(matrix, result, 
                             s_row + 1, s_col + 1, 
                             e_row - 1, e_col - 1, k);
		}
	}
	fun element_size(c: Int): Int
	{
		var counter: Int = 0;
      	var col = c;
		while (col > 0)
		{
			counter += col;
			col -= 1;
		}
		return counter * 4;
	}
	fun diamond_spiral(matrix: Array < Array < Int >> , row: Int): Unit
	{
		if (row % 2 == 0)
		{
			print("\nNot a valid perfect Odd square matrix");
			return;
		}
		val size: Int = this.element_size((row) / 2) + 1;
		val result: Array < Int > = Array(size)
		{
			0
		};
		this.spiral_view(matrix, result, 0, 0, 
                         row - 1, row - 1, size);
		var i: Int = 0;
		while (i < size)
		{
			print("  " + result[i]);
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Spiral = Spiral();
	// Matrix of integer elements
	val matrix: Array < Array < Int >> = arrayOf(
      arrayOf(1, 2, 3, 4, 5, 6, 7), 
      arrayOf(8, 9, 10, 11, 12, 13, 14), 
      arrayOf(15, 16, 17, 18, 19, 20, 21), 
      arrayOf(22, 23, 24, 25, 26, 27, 28), 
      arrayOf(29, 30, 31, 32, 33, 34, 35), 
      arrayOf(36, 37, 38, 39, 40, 41, 42), 
      arrayOf(43, 44, 45, 46, 47, 48, 49)
    );
	// Get the length of Odd square matrix
	val size: Int = matrix.count();
	// Display diamond view
	task.diamond_view(matrix, size);
	// Display diamond spiral view
	task.diamond_spiral(matrix, size);
}

Output

           4
        10 11 12
     16 17 18 19 20
  22 23 24 25 26 27 28
     30 31 32 33 34
        38 39 40
           46
  25  24  32  26  18  17  23  31  39  33  27  19  11  10  16  22  30  38  46  40  34  28  20  12  4

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