diff --git a/2D Arrays/Diagonal Boundary Spiral Traversal of a 2D Array/Diagonal.c b/2D Arrays/Diagonal Boundary Spiral Traversal of a 2D Array/Diagonal.c new file mode 100644 index 00000000..0a8d8f93 --- /dev/null +++ b/2D Arrays/Diagonal Boundary Spiral Traversal of a 2D Array/Diagonal.c @@ -0,0 +1,57 @@ +#include + +void diagonalBoundarySpiral(int arr[][100], int rows, int cols) { + int top = 0, bottom = rows - 1, left = 0, right = cols - 1; + + while (top <= bottom && left <= right) { + // Top-left to bottom-right diagonal + for (int i = top, j = left; i <= bottom && j <= right; i++, j++) { + printf("%d ", arr[i][j]); + } + top++; // Move top boundary down + + // Top-right to bottom-left diagonal + for (int i = top, j = right; i <= bottom && j >= left; i++, j--) { + printf("%d ", arr[i][j]); + } + right--; // Move right boundary left + + if (top <= bottom) { + // Bottom-right to top-left diagonal + for (int i = bottom, j = right; i >= top && j >= left; i--, j--) { + printf("%d ", arr[i][j]); + } + bottom--; // Move bottom boundary up + } + + if (left <= right) { + // Bottom-left to top-right diagonal + for (int i = bottom, j = left; i >= top && j <= right; i--, j++) { + printf("%d ", arr[i][j]); + } + left++; // Move left boundary right + } + } +} + +int main() { + int arr[100][100], rows, cols; + + // Input dimensions of the 2D array + printf("Enter the number of rows and columns: "); + scanf("%d %d", &rows, &cols); + + // Input elements of the 2D array + printf("Enter the elements of the array:\n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + scanf("%d", &arr[i][j]); + } + } + + // Perform diagonal boundary spiral traversal + printf("Diagonal Boundary Spiral Traversal: "); + diagonalBoundarySpiral(arr, rows, cols); + + return 0; +} diff --git a/2D Arrays/Diagonal Boundary Spiral Traversal of a 2D Array/readme.md b/2D Arrays/Diagonal Boundary Spiral Traversal of a 2D Array/readme.md new file mode 100644 index 00000000..aae7c591 --- /dev/null +++ b/2D Arrays/Diagonal Boundary Spiral Traversal of a 2D Array/readme.md @@ -0,0 +1,41 @@ +# Diagonal Boundary Spiral Traversal of a 2D Array in C + +This C program performs a diagonal boundary spiral traversal of a 2D array. It moves along the matrix's outer boundaries in a diagonal pattern until all elements are traversed. + +# Description +The program accepts a 2D array of fixed maximum size, though its effective size (rows and columns) can be specified by the user. The diagonal boundary spiral traversal involves moving along the matrix's outermost boundaries diagonally, starting from the top-left corner and spiraling inward. + +# Features +Traverses a 2D array in a spiral diagonal pattern. +Moves along four primary diagonals (top-left to bottom-right, top-right to bottom-left, bottom-right to top-left, and bottom-left to top-right) and narrows the boundaries after each traversal. +Outputs all elements in the specified traversal order. +Usage +Clone the repository or download the source code. +Open the code in your preferred C compiler or IDE. +Enter the desired row and column dimensions, followed by the elements of the array. +Compile and run the program. +# Example +For an array: + +| 1 2 3 | +| 4 5 6 | +| 7 8 9 | + +The output will be the elements in a diagonal boundary spiral order: 1 5 9 6 3 2 4 8 7 + +# Time and Space Complexity +Time Complexity +The time complexity of this diagonal boundary spiral traversal algorithm is: +O(m × n), where m is the number of rows and n is the number of columns in the array. This complexity arises from the need to visit each element once. +Space Complexity +The space complexity of the algorithm is: +O(1) if we are printing directly, as it only requires a few pointers for traversal and boundaries. +O(m × n) if storing the traversal output in an array, as additional storage is needed for output elements. +# Requirements +A C compiler (e.g., GCC) +Basic knowledge of C programming + + + + +