Program to Traverse Matrix Helically
- Write a program to Traverse Matrix Helically in C
- Write a program to Traverse Matrix Helically in C++
- Write a program to Traverse Matrix Helically in Python
- Write a program to Traverse Matrix Helically in PHP
- Write a program to Traverse Matrix Helically in Java
- Write a program to Traverse Matrix Helically in Java Script
- Write a program to Traverse Matrix Helically in C#
Explanation:
Steps:
- Input:
- A m×n matrix.
- Define Boundaries:
- Use variables to track the current boundaries of the matrix:
- top: The starting row.
- bottom: The ending row.
- left: The starting column.
- right: The ending column.
- Use variables to track the current boundaries of the matrix:
- Traverse the Matrix:
- Traverse elements within the current boundaries in the following order:
- Left to right across the top row.
- Top to bottom along the right column.
- Right to left across the bottom row (if it hasn’t been visited).
- Bottom to top along the left column (if it hasn’t been visited).
- Adjust the boundaries after each traversal to move inward.
- Traverse elements within the current boundaries in the following order:
- Stop Condition:
- Stop when all rows and columns have been traversed (when top > bottom or left > right).
- Output:
- Print the elements in the spiral order.
Program to Traverse Matrix Helically
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-c">#include <stdio.h> void printSpiral(int matrix[3][3], int rows, int cols) { int top = 0, bottom = rows - 1, left = 0, right = cols - 1; printf("Helical traversal of the matrix:\n"); while (top <= bottom && left <= right) { // Traverse top row for (int i = left; i <= right; i++) printf("%d ", matrix[top][i]); top++; // Traverse right column for (int i = top; i <= bottom; i++) printf("%d ", matrix[i][right]); right--; // Traverse bottom row (if any) if (top <= bottom) { for (int i = right; i >= left; i--) printf("%d ", matrix[bottom][i]); bottom--; } // Traverse left column (if any) if (left <= right) { for (int i = bottom; i >= top; i--) printf("%d ", matrix[i][left]); left++; } } } int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; printSpiral(matrix, 3, 3); return 0; }
#include <iostream> using namespace std; void printSpiral(int matrix[3][3], int rows, int cols) { int top = 0, bottom = rows - 1, left = 0, right = cols - 1; cout << "Helical traversal of the matrix:" << endl; while (top <= bottom && left <= right) { // Traverse top row for (int i = left; i <= right; i++) cout << matrix[top][i] << " "; top++; // Traverse right column for (int i = top; i <= bottom; i++) cout << matrix[i][right] << " "; right--; // Traverse bottom row (if any) if (top <= bottom) { for (int i = right; i >= left; i--) cout << matrix[bottom][i] << " "; bottom--; } // Traverse left column (if any) if (left <= right) { for (int i = bottom; i >= top; i--) cout << matrix[i][left] << " "; left++; } } } int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; printSpiral(matrix, 3, 3); return 0; }
def print_spiral(matrix): top, bottom = 0, len(matrix) - 1 left, right = 0, len(matrix[0]) - 1 print("Helical traversal of the matrix:") while top <= bottom and left <= right: # Traverse top row for i in range(left, right + 1): print(matrix[top][i], end=" ") top += 1 # Traverse right column for i in range(top, bottom + 1): print(matrix[i][right], end=" ") right -= 1 # Traverse bottom row (if any) if top <= bottom: for i in range(right, left - 1, -1): print(matrix[bottom][i], end=" ") bottom -= 1 # Traverse left column (if any) if left <= right: for i in range(bottom, top - 1, -1): print(matrix[i][left], end=" ") left += 1 matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print_spiral(matrix)
<?php function printSpiral($matrix) { $top = 0; $bottom = count($matrix) - 1; $left = 0; $right = count($matrix[0]) - 1; echo "Helical traversal of the matrix:\n"; while ($top <= $bottom && $left <= $right) { // Traverse top row for ($i = $left; $i <= $right; $i++) echo $matrix[$top][$i] . " "; $top++; // Traverse right column for ($i = $top; $i <= $bottom; $i++) echo $matrix[$i][$right] . " "; $right--; // Traverse bottom row (if any) if ($top <= $bottom) { for ($i = $right; $i >= $left; $i--) echo $matrix[$bottom][$i] . " "; $bottom--; } // Traverse left column (if any) if ($left <= $right) { for ($i = $bottom; $i >= $top; $i--) echo $matrix[$i][$left] . " "; $left++; } } } $matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; printSpiral($matrix); ?>
public class SpiralMatrix { public static void main(String[] args) { int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int top = 0, bottom = 2, left = 0, right = 2; System.out.println("Helical traversal of the matrix:"); while (top <= bottom && left <= right) { // Traverse top row for (int i = left; i <= right; i++) System.out.print(matrix[top][i] + " "); top++; // Traverse right column for (int i = top; i <= bottom; i++) System.out.print(matrix[i][right] + " "); right--; // Traverse bottom row (if any) if (top <= bottom) { for (int i = right; i >= left; i--) System.out.print(matrix[bottom][i] + " "); bottom--; } // Traverse left column (if any) if (left <= right) { for (int i = bottom; i >= top; i--) System.out.print(matrix[i][left] + " "); left++; } } } }
function printSpiral(matrix) { let top = 0, bottom = matrix.length - 1; let left = 0, right = matrix[0].length - 1; console.log("Helical traversal of the matrix:"); while (top <= bottom && left <= right) { // Traverse top row for (let i = left; i <= right; i++) console.log(matrix[top][i]); top++; // Traverse right column for (let i = top; i <= bottom; i++) console.log(matrix[i][right]); right--; // Traverse bottom row (if any) if (top <= bottom) { for (let i = right; i >= left; i--) console.log(matrix[bottom][i]); bottom--; } // Traverse left column (if any) if (left <= right) { for (let i = bottom; i >= top; i--) console.log(matrix[i][left]); left++; } } } let matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; printSpiral(matrix);
using System; class SpiralMatrix { static void Main() { int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int top = 0, bottom = 2, left = 0, right = 2; Console.WriteLine("Helical traversal of the matrix:"); while (top <= bottom && left <= right) { // Traverse top row for (int i = left; i <= right; i++) Console.Write(matrix[top, i] + " "); top++; // Traverse right column for (int i = top; i <= bottom; i++) Console.Write(matrix[i, right] + " "); right--; // Traverse bottom row (if any) if (top <= bottom) { for (int i = right; i >= left; i--) Console.Write(matrix[bottom, i] + " "); bottom--; } // Traverse left column (if any) if (left <= right) { for (int i = bottom; i >= top; i--) Console.Write(matrix[i, left] + " "); left++; } } } }