Traverse matrix in L shape
The problem involves traversing a matrix in an "L" shape pattern. Given a matrix, the task is to print its elements in an L-shaped pattern. The L-shaped pattern starts from the top-left corner and moves downward and then to the right, forming an L shape.
Example
Consider the following matrix:
4 6 -2 2
3 -1 5 3
1 4 4 6
9 4 6 2
1 2 8 9
The L-shaped traversal of this matrix would result in:
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
Idea to Solve the Problem
To traverse the matrix in the specified L shape pattern, we can use three nested loops. The outer loop controls the columns of the matrix, the first inner loop controls the rows in the downward direction, and the second inner loop controls the rows in the rightward direction.
Algorithm
- For each column
c
from0
toM-1
, do the following: a. For each rowr
from0
to(N-1) - c
, printmatrix[r][c]
. b. For each columnk
fromc
toM-1
, printmatrix[r][k]
. - Print a newline to move to the next L-shaped portion.
Pseudocode
print_l_shape(matrix):
for c from 0 to M-1:
for r from 0 to (N-1) - c:
print matrix[r][c]
for k from c to M-1:
print matrix[r][k]
print newline
main:
matrix = ... // Define the matrix
print_l_shape(matrix)
Code Solution
// C Program
// Traverse matrix in L shape
#include <stdio.h>
#define M 4 // col
#define N 5 // row
// Print the L shape in given matrix
void print_l_shape(int matrix[N][M])
{
printf(" L Shapes \n");
// Loop controlling variables
int c = 0;
int r = 0;
int k =0;
for(c = 0;c < M; c++)
{
for(r = 0; r < (N-1)-c; r++)
{
printf(" %d",matrix[r][c]);
}
for(k = c;k < M;k++)
{
printf(" %d",matrix[r][k]);
}
printf("\n");
}
printf("\n");
}
int main()
{
// Define matrix of integer elements
int matrix[N][M] =
{
{4, 6, -2 , 2 },
{3, -1, 5 , 3 },
{1, 4, 4, 6 },
{9, 4, 6, 2 },
{1, 2, 8, 9 }
};
print_l_shape(matrix);
return 0;
}
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
/*
Java Program
Traverse matrix in L shape
*/
class MyMatrix
{
// Print the L shape in given matrix
public void print_l_shape(int[][] matrix)
{
// Get the size
int row = matrix.length;
int col = matrix[0].length;
System.out.print(" L Shapes \n");
// Loop controlling variables
int c = 0;
int r = 0;
int k = 0;
for (c = 0; c < col; c++)
{
for (r = 0; r < (row - 1) - c; r++)
{
System.out.print(" " + matrix[r][c]);
}
for (k = c; k < col; k++)
{
System.out.print(" " + matrix[r][k]);
}
System.out.print("\n");
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyMatrix obj = new MyMatrix();
// Case 1
// Define matrix of integer elements
int[][] matrix1 =
{
{4, 6, -2 , 2 },
{3, -1, 5 , 3 },
{1, 4, 4, 6 },
{9, 4, 6, 2 },
{1, 2, 8, 9 }
};
obj.print_l_shape(matrix1);
// Case 2
// Define matrix of integer elements
int[][] matrix2 =
{
{1, 9, 8 , 3 , 7},
{3, -1, 5 , 3 , 8},
{1, 3, 4, 6 , 11},
{0, 8, 8, 6 , 2},
{1, 3, 4, 5 , 10}
};
obj.print_l_shape(matrix2);
}
}
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
// Include header file
#include <iostream>
#define M 4 // col
#define N 5 // row
using namespace std;
/*
C++ Program
Traverse matrix in L shape
*/
class MyMatrix
{
public:
// Print the L shape in given matrix
void print_l_shape(int matrix[N][M])
{
cout << " L Shapes \n";
// Loop controlling variables
int c = 0;
int r = 0;
int k = 0;
for (c = 0; c < M; c++)
{
for (r = 0; r < (N - 1) - c; r++)
{
cout << " " << matrix[r][c];
}
for (k = c; k < M; k++)
{
cout << " " << matrix[r][k];
}
cout << "\n";
}
cout << "\n";
}
};
int main()
{
MyMatrix obj = MyMatrix();
// Define matrix of integer elements
int matrix[N][M] =
{
{4, 6, -2 , 2 },
{3, -1, 5 , 3 },
{1, 4, 4, 6 },
{9, 4, 6, 2 },
{1, 2, 8, 9 }
};
obj.print_l_shape(matrix);
return 0;
}
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
// Include namespace system
using System;
/*
C# Program
Traverse matrix in L shape
*/
public class MyMatrix
{
// Print the L shape in given matrix
public void print_l_shape(int[,] matrix)
{
// Get the size
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
Console.Write(" L Shapes \n");
// Loop controlling variables
int c = 0;
int r = 0;
int k = 0;
for (c = 0; c < col; c++)
{
for (r = 0; r < (row - 1) - c; r++)
{
Console.Write(" " + matrix[r,c]);
}
for (k = c; k < col; k++)
{
Console.Write(" " + matrix[r,k]);
}
Console.Write("\n");
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyMatrix obj = new MyMatrix();
// Case 1
// Define matrix of integer elements
int[,] matrix1 =
{
{4, 6, -2 , 2 },
{3, -1, 5 , 3 },
{1, 4, 4, 6 },
{9, 4, 6, 2 },
{1, 2, 8, 9 }
};
obj.print_l_shape(matrix1);
// Case 2
// Define matrix of integer elements
int[,] matrix2 =
{
{1, 9, 8 , 3 , 7},
{3, -1, 5 , 3 , 8},
{1, 3, 4, 6 , 11},
{0, 8, 8, 6 , 2},
{1, 3, 4, 5 , 10}
};
obj.print_l_shape(matrix2);
}
}
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
<?php
/*
Php Program
Traverse matrix in L shape
*/
class MyMatrix
{
// Print the L shape in given matrix
public function print_l_shape($matrix)
{
// Get the size
$row = count($matrix);
$col = count($matrix[0]);
echo " L Shapes \n";
// Loop controlling variables
$c = 0;
$r = 0;
$k = 0;
for ($c = 0; $c < $col; $c++)
{
for ($r = 0; $r < ($row - 1) - $c; $r++)
{
echo " ". $matrix[$r][$c];
}
for ($k = $c; $k < $col; $k++)
{
echo " ". $matrix[$r][$k];
}
echo "\n";
}
echo "\n";
}
}
function main()
{
$obj = new MyMatrix();
// Case 1
// Define matrix of integer elements
$matrix1 = array(
array(4, 6, -2, 2),
array(3, -1, 5, 3),
array(1, 4, 4, 6),
array(9, 4, 6, 2),
array(1, 2, 8, 9)
);
$obj->print_l_shape($matrix1);
// Case 2
// Define matrix of integer elements
$matrix2 = array(
array(1, 9, 8, 3, 7),
array(3, -1, 5, 3, 8),
array(1, 3, 4, 6, 11),
array(0, 8, 8, 6, 2),
array(1, 3, 4, 5, 10)
);
$obj->print_l_shape($matrix2);
}
main();
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
/*
Node Js Program
Traverse matrix in L shape
*/
class MyMatrix
{
// Print the L shape in given matrix
print_l_shape(matrix)
{
// Get the size
var row = matrix.length;
var col = matrix[0].length;
process.stdout.write(" L Shapes \n");
// Loop controlling variables
var c = 0;
var r = 0;
var k = 0;
for (c = 0; c < col; c++)
{
for (r = 0; r < (row - 1) - c; r++)
{
process.stdout.write(" " + matrix[r][c]);
}
for (k = c; k < col; k++)
{
process.stdout.write(" " + matrix[r][k]);
}
process.stdout.write("\n");
}
process.stdout.write("\n");
}
}
function main()
{
var obj = new MyMatrix();
// Case 1
// Define matrix of integer elements
var matrix1 = [
[4, 6, -2, 2] ,
[3, -1, 5, 3] ,
[1, 4, 4, 6] ,
[9, 4, 6, 2] ,
[1, 2, 8, 9]
];
obj.print_l_shape(matrix1);
// Case 2
// Define matrix of integer elements
var matrix2 = [
[1, 9, 8, 3, 7] ,
[3, -1, 5, 3, 8] ,
[1, 3, 4, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
];
obj.print_l_shape(matrix2);
}
main();
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
# Python 3 Program
# Traverse matrix in L shape
class MyMatrix :
# Print the L shape in given matrix
def print_l_shape(self, matrix) :
# Get the size
row = len(matrix)
col = len(matrix[0])
print(" L Shapes \n", end = "")
# Loop controlling variables
c = 0
r = 0
k = 0
while (c < col) :
r = 0
while (r < (row - 1) - c) :
print(" ", matrix[r][c], end = "")
r += 1
k = c
while (k < col) :
print(" ", matrix[r][k], end = "")
k += 1
print("\n", end = "")
c += 1
print("\n", end = "")
def main() :
obj = MyMatrix()
# Case 1
# Define matrix of integer elements
matrix1 = [
[4, 6, -2, 2] ,
[3, -1, 5, 3] ,
[1, 4, 4, 6] ,
[9, 4, 6, 2] ,
[1, 2, 8, 9]
]
obj.print_l_shape(matrix1)
# Case 2
# Define matrix of integer elements
matrix2 = [
[1, 9, 8, 3, 7] ,
[3, -1, 5, 3, 8] ,
[1, 3, 4, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
]
obj.print_l_shape(matrix2)
if __name__ == "__main__": main()
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
# Ruby Program
# Traverse matrix in L shape
class MyMatrix
# Print the L shape in given matrix
def print_l_shape(matrix)
# Get the size
row = matrix.length
col = matrix[0].length
print(" L Shapes \n")
# Loop controlling variables
c = 0
r = 0
k = 0
while (c < col)
r = 0
while (r < (row - 1) - c)
print(" ", matrix[r][c])
r += 1
end
k = c
while (k < col)
print(" ", matrix[r][k])
k += 1
end
print("\n")
c += 1
end
print("\n")
end
end
def main()
obj = MyMatrix.new()
# Case 1
# Define matrix of integer elements
matrix1 = [
[4, 6, -2, 2] ,
[3, -1, 5, 3] ,
[1, 4, 4, 6] ,
[9, 4, 6, 2] ,
[1, 2, 8, 9]
]
obj.print_l_shape(matrix1)
# Case 2
# Define matrix of integer elements
matrix2 = [
[1, 9, 8, 3, 7] ,
[3, -1, 5, 3, 8] ,
[1, 3, 4, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
]
obj.print_l_shape(matrix2)
end
main()
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
/*
Scala Program
Traverse matrix in L shape
*/
class MyMatrix
{
// Print the L shape in given matrix
def print_l_shape(matrix: Array[Array[Int]]): Unit = {
// Get the size
var row: Int = matrix.length;
var col: Int = matrix(0).length;
print(" L Shapes \n");
// Loop controlling variables
var c: Int = 0;
var r: Int = 0;
var k: Int = 0;
while (c < col)
{
r = 0;
while (r < (row - 1) - c)
{
print(" " + matrix(r)(c));
r += 1;
}
k = c;
while (k < col)
{
print(" " + matrix(r)(k));
k += 1;
}
print("\n");
c += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMatrix = new MyMatrix();
// Case 1
// Define matrix of integer elements
var matrix1: Array[Array[Int]] = Array(
Array(4, 6, -2, 2),
Array(3, -1, 5, 3),
Array(1, 4, 4, 6),
Array(9, 4, 6, 2),
Array(1, 2, 8, 9)
);
obj.print_l_shape(matrix1);
// Case 2
// Define matrix of integer elements
var matrix2: Array[Array[Int]] = Array(
Array(1, 9, 8, 3, 7),
Array(3, -1, 5, 3, 8),
Array(1, 3, 4, 6, 11),
Array(0, 8, 8, 6, 2),
Array(1, 3, 4, 5, 10)
);
obj.print_l_shape(matrix2);
}
}
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
/*
Swift 4 Program
Traverse matrix in L shape
*/
class MyMatrix
{
// Print the L shape in given matrix
func print_l_shape(_ matrix: [[Int]])
{
// Get the size
let row: Int = matrix.count;
let col: Int = matrix[0].count;
print(" L Shapes \n", terminator: "");
// Loop controlling variables
var c: Int = 0;
var r: Int = 0;
var k: Int = 0;
while (c < col)
{
r = 0;
while (r < (row - 1) - c)
{
print(" ", matrix[r][c], terminator: "");
r += 1;
}
k = c;
while (k < col)
{
print(" ", matrix[r][k], terminator: "");
k += 1;
}
print("\n", terminator: "");
c += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyMatrix = MyMatrix();
// Case 1
// Define matrix of integer elements
let matrix1: [[Int]] = [
[4, 6, -2, 2] ,
[3, -1, 5, 3] ,
[1, 4, 4, 6] ,
[9, 4, 6, 2] ,
[1, 2, 8, 9]
];
obj.print_l_shape(matrix1);
// Case 2
// Define matrix of integer elements
let matrix2: [[Int]] = [
[1, 9, 8, 3, 7] ,
[3, -1, 5, 3, 8] ,
[1, 3, 4, 6, 11] ,
[0, 8, 8, 6, 2] ,
[1, 3, 4, 5, 10]
];
obj.print_l_shape(matrix2);
}
main();
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
/*
Kotlin Program
Traverse matrix in L shape
*/
class MyMatrix
{
// Print the L shape in given matrix
fun print_l_shape(matrix: Array<Array<Int>>): Unit
{
// Get the size
var row: Int = matrix.count();
var col: Int = matrix[0].count();
print(" L Shapes \n");
// Loop controlling variables
var c: Int = 0;
var r: Int ;
var k: Int ;
while (c < col)
{
r = 0;
while (r < (row - 1) - c)
{
print(" " + matrix[r][c]);
r += 1;
}
k = c;
while (k < col)
{
print(" " + matrix[r][k]);
k += 1;
}
print("\n");
c += 1;
}
print("\n");
}
}
fun main(args: Array<String> ): Unit
{
var obj: MyMatrix = MyMatrix();
// Case 1
// Define matrix of integer elements
var matrix1: Array <Array<Int>> = arrayOf(
arrayOf(4, 6, -2, 2),
arrayOf(3, -1, 5, 3),
arrayOf(1, 4, 4, 6),
arrayOf(9, 4, 6, 2),
arrayOf(1, 2, 8, 9));
obj.print_l_shape(matrix1);
// Case 2
// Define matrix of integer elements
var matrix2: Array<Array<Int>> = arrayOf(
arrayOf(1, 9, 8, 3, 7),
arrayOf(3, -1, 5, 3, 8),
arrayOf(1, 3, 4, 6, 11),
arrayOf(0, 8, 8, 6, 2),
arrayOf(1, 3, 4, 5, 10));
obj.print_l_shape(matrix2);
}
Output
L Shapes
4 3 1 9 1 2 8 9
6 -1 4 4 6 2
-2 5 4 6
2 3
L Shapes
1 3 1 0 1 3 4 5 10
9 -1 3 8 8 6 2
8 5 4 6 11
3 3 8
7
Output Explanation
The mentioned C code implements the above algorithm to traverse the matrix in the L-shaped pattern. It uses nested loops to iterate through the rows and columns of the matrix, printing elements in the L shape. The output demonstrates the L-shaped traversal of the matrix.
Time Complexity
The time complexity of the algorithm is O(N*M), where N is the number of rows and M is the number of columns in the matrix. This is because the algorithm involves traversing each element of the matrix once. The nested loops iterate through all the rows and columns, resulting in a linear time complexity proportional to the size of the matrix.
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