Print matrix in z form
The problem is to print the elements of a given square matrix in "Z" form. The "Z" form traversal starts from the top-left corner and goes to the bottom-right corner, zigzagging through the matrix. The traversal alternates between going from left to right in the rows and from right to left in the diagonal.
Example
Consider the following 6x6 matrix:
3 4 5 6 7 8
58 41 38 18 9 12
55 42 37 10 13 33
50 43 11 20 12 54
49 12 35 21 11 65
13 45 34 31 10 14
The "Z" form traversal of this matrix would be:
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
Idea to Solve the Problem
To print the matrix in "Z" form, we can follow these steps:
- Traverse the matrix using a loop.
- If we are in the first row or the last row of the matrix, print the entire row from left to right.
- If we are in any other row, print only the diagonal element from top-right to bottom-left.
Algorithm
- Create a function
z_form(matrix)
to perform the "Z" form printing. - Initialize
size
as the number of rows (assuming it is a square matrix) andi = 0
,j = 0
, andc = size - 1
. - Loop through the matrix using the variable
i
from 0 tosize
and decrementc
by 1 in each iteration. - Check if
i
is 0 or equal tosize - 1
. If true, print the entire row from left to right. Otherwise, print only the diagonal element from top-right to bottom-left. - Continue this process until all elements in the "Z" form are printed.
Pseudocode
z_form(matrix):
size = length of matrix (assuming it's a square matrix)
i = 0, j = 0, c = size - 1
for i from 0 to size and decrement c by 1 in each iteration:
if i == 0 or i == size - 1:
print entire row from left to right
else:
print diagonal element from top-right to bottom-left
Code Solution
/*
C Program
Print matrix in z form
*/
#include <stdio.h>
#define SIZE 6
void z_form(int matrix[SIZE][SIZE])
{
int i = 0, j = 0, c = SIZE - 1;
for (i = 0; i < SIZE && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == SIZE)
{
//display first and last rows
for (j = 0; j < SIZE; ++j)
{
printf("%d ", matrix[i][j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
printf("%d ", matrix[i][c]);
}
}
}
int main()
{
//Define matrix elements
int matrix[SIZE][SIZE] = {
{3, 4, 5, 6, 7, 8},
{58,41,38,18,9, 12},
{55,42,37,10,13,33},
{50,43,11,20,12,54},
{49,12,35,21,11,65},
{13,45,34,31,10,14}
};
z_form(matrix);
return 0;
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Java Program
Print matrix in z form
*/
class MyMatrix
{
public void z_form(int[][] matrix)
{
int size = matrix.length;
if (size != matrix[0].length)
{
// not NxN matrix
return;
}
int i = 0, j = 0, c = size - 1;
for (i = 0; i < size && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; ++j)
{
System.out.print(" " + matrix[i][j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
System.out.print(" " + matrix[i][c]);
}
}
}
public static void main(String[] args)
{
MyMatrix obj = new MyMatrix();
int[][] matrix = {
{3, 4, 5, 6, 7, 8},
{58,41,38,18,9, 12},
{55,42,37,10,13,33},
{50,43,11,20,12,54},
{49,12,35,21,11,65},
{13,45,34,31,10,14}
};
obj.z_form(matrix);
}
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
C++ Program
Print matrix in z form
*/
#include<iostream>
#define SIZE 6
using namespace std;
class MyMatrix
{
public: void z_form(int matrix[SIZE][SIZE])
{
//Accept NxN matrix
int size = SIZE;
int i = 0, j = 0, c = size - 1;
for (i = 0; i < size && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; ++j)
{
cout << " " << matrix[i][j];
}
}
else
{
cout << " " << matrix[i][c];
}
}
}
};
int main()
{
MyMatrix obj = MyMatrix();
int matrix[SIZE][SIZE] = {
{3, 4, 5, 6, 7, 8},
{58,41,38,18,9, 12},
{55,42,37,10,13,33},
{50,43,11,20,12,54},
{49,12,35,21,11,65},
{13,45,34,31,10,14}
};
obj.z_form(matrix);
return 0;
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
C# Program
Print matrix in z form
*/
using System;
class MyMatrix
{
public void z_form(int[,] matrix)
{
int size = matrix.GetLength(0);
if (size != matrix.GetLength(1)){
// not NxN matrix
return;
}
int i = 0, j = 0, c = size - 1;
for (i = 0; i < size && c >= 0; i++, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; j++)
{
Console.Write(" " + matrix[i,j]);
}
}
else
{
Console.Write(" " + matrix[i,c]);
}
}
}
public static void Main(String[] args)
{
MyMatrix obj = new MyMatrix();
int[,] matrix = {
{
3,
4,
5,
6,
7,
8
},
{
58,
41,
38,
18,
9,
12
},
{
55,
42,
37,
10,
13,
33
},
{
50,
43,
11,
20,
12,
54
},
{
49,
12,
35,
21,
11,
65
},
{
13,
45,
34,
31,
10,
14
}
};
obj.z_form(matrix);
}
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
<?php
/*
Php Program
Print matrix in z form
*/
class MyMatrix
{
public function z_form( & $matrix)
{
$size = count($matrix);
if ($size != count($matrix[0]))
{
return;
}
$i = 0;
$j = 0;
$c = $size - 1;
for ($i = 0; $i < $size && $c >= 0; ++$i, $c--)
{
if ($i == 0 || $i + 1 == $size)
{
//display first and last rows
for ($j = 0; $j < $size; ++$j)
{
echo(" ". $matrix[$i][$j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
echo(" ". $matrix[$i][$c]);
}
}
}
}
function main()
{
$obj = new MyMatrix();
$matrix = array(
array(3, 4, 5, 6, 7, 8),
array(58, 41, 38, 18, 9, 12),
array(55, 42, 37, 10, 13, 33),
array(50, 43, 11, 20, 12, 54),
array(49, 12, 35, 21, 11, 65),
array(13, 45, 34, 31, 10, 14));
$obj->z_form($matrix);
}
main();
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Node Js Program
Print matrix in z form
*/
class MyMatrix
{
z_form(matrix)
{
var size = matrix.length;
if (size != matrix[0].length)
{
return;
}
var i = 0;
var j = 0;
var c = size - 1;
for (i = 0; i < size && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; ++j)
{
process.stdout.write(" " + matrix[i][j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
process.stdout.write(" " + matrix[i][c]);
}
}
}
}
function main(args)
{
var obj = new MyMatrix();
var matrix = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
];
obj.z_form(matrix);
}
main();
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
#
# Python 3 Program
# Print matrix in z form
class MyMatrix :
def z_form(self, matrix) :
size = len(matrix)
if (size != len(matrix[0])) :
return
i = 0
j = 0
c = size - 1
while (i < size and c >= 0) :
if (i == 0 or i + 1 == size) :
# display first and last rows
j = 0
while (j < size) :
print(" ", matrix[i][j], end = "")
j += 1
else :
print(" ", matrix[i][c], end = "")
i += 1
c -= 1
def main() :
obj = MyMatrix()
matrix = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
]
obj.z_form(matrix)
if __name__ == "__main__": main()
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
# Ruby Program
# Print matrix in z form
class MyMatrix
def z_form(matrix)
size = matrix.length
if (size != matrix[0].length)
return
end
i = 0
j = 0
c = size - 1
while (i < size && c >= 0)
if (i == 0 || i + 1 == size)
# display first and last rows
j = 0
while (j < size)
print(" ", matrix[i][j])
j += 1
end
else
print(" ", matrix[i][c])
end
i += 1
c -= 1
end
end
end
def main()
obj = MyMatrix.new()
matrix = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
]
obj.z_form(matrix)
end
main()
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Scala Program
Print matrix in z form
*/
class MyMatrix
{
def z_form(matrix: Array[Array[Int]]): Unit = {
var size: Int = matrix.length;
if (size != matrix(0).length)
{
return;
}
var i: Int = 0;
var j: Int = 0;
var c: Int = size - 1;
while (i < size && c >= 0)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
j = 0;
while (j < size)
{
print(" " + matrix(i)(j));
j += 1;
}
}
else
{
print(" " + matrix(i)(c));
}
i += 1;
c -= 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMatrix = new MyMatrix();
var matrix: Array[Array[Int]] = Array(
Array(3, 4, 5, 6, 7, 8),
Array(58, 41, 38, 18, 9, 12),
Array(55, 42, 37, 10, 13, 33),
Array(50, 43, 11, 20, 12, 54),
Array(49, 12, 35, 21, 11, 65),
Array(13, 45, 34, 31, 10, 14));
obj.z_form(matrix);
}
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Swift Program
Print matrix in z form
*/
class MyMatrix
{
func z_form(_ matrix: [
[Int]
])
{
let size: Int = matrix.count;
if (size != matrix[0].count)
{
return;
}
var i: Int = 0;
var j: Int = 0;
var c: Int = size - 1;
while (i < size && c >= 0)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
j = 0;
while (j < size)
{
print(" ", matrix[i][j], terminator: "");
j += 1;
}
}
else
{
print(" ", matrix[i][c], terminator: "");
}
i += 1;
c -= 1;
}
}
}
func main()
{
let obj: MyMatrix = MyMatrix();
let matrix: [
[Int]
] = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
];
obj.z_form(matrix);
}
main();
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
Output Explanation
The given Java code implements the above algorithm to print the matrix in "Z" form. It prints the elements in the "Z" form traversal, as described in the output.
Time Complexity
The time complexity of the provided solution is O(N), where N is the number of rows (or columns) in the square matrix. The function traverses the matrix once, and in each iteration, it performs constant time operations to print the elements. 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