1
+ /*
2
+ Write a function that takes in an n x m two-dimensional array (that can be square-shaped when n == m) and
3
+ returns a one-dimensional array of all the array's elements in spiral order.
4
+
5
+ Spiral order starts at the top left corner of the two-dimensional array, goes to the right, and proceeds
6
+ in a spiral pattern all the way until every element has been visited.
7
+
8
+ Explanation:
9
+
10
+ The SpiralTraverse function takes a 2D integer array array and returns a 1D integer slice that contains the
11
+ elements of array traversed in a spiral order, starting from the top-left corner and moving clockwise.
12
+
13
+ The function first initializes an empty slice result to hold the elements of the spiral traversal.
14
+ If the input array is empty, the function immediately returns the empty result.
15
+
16
+ Next, the function initializes variables startRow, endRow, startCol, and endCol to keep track of the
17
+ boundaries of the matrix. These variables will be updated as the function traverses the matrix.
18
+
19
+ The function then enters a loop that traverses the matrix in a spiral order. The loop continues
20
+ as long as startRow <= endRow and startCol <= endCol, which means that there are still elements
21
+ in the matrix to be traversed.
22
+
23
+ The first step in the loop is to traverse the top row of the matrix from left to right, and append
24
+ each element to the result slice. The next step is to traverse the rightmost column of the matrix from top to bottom,
25
+ and append each element to the result slice. If there is more than one row in the matrix, the function then traverses
26
+ the bottom row of the matrix from right to left, and appends each element to the result slice. If there is only one row left,
27
+ the loop is broken to avoid duplicating the elements. Finally, if there is more than one column in the matrix,
28
+ the function traverses the left
29
+
30
+ O(n) time | O(n) space - where n is the total number of elements in the array
31
+ */
32
+ #include < bits/stdc++.h>
33
+ using namespace std ;
34
+
35
+ #include < vector>
36
+ using namespace std ;
37
+
38
+ vector<int > SpiralTraverse (vector<vector<int >> array) {
39
+ vector<int > result; // vector to store the spiral traversal
40
+ int rows = array.size (); // number of rows in the input array
41
+ int cols = array[0 ].size (); // number of columns in the input array
42
+ int startRow = 0 , endRow = rows - 1 ; // indices for the start and end row of the current subarray
43
+ int startCol = 0 , endCol = cols - 1 ; // indices for the start and end column of the current subarray
44
+
45
+ // loop until the entire input array is traversed
46
+ while (startRow <= endRow && startCol <= endCol) {
47
+ // traverse the top row from left to right
48
+ for (int col = startCol; col <= endCol; col++) {
49
+ result.push_back (array[startRow][col]);
50
+ }
51
+ // traverse the right column from top to bottom
52
+ for (int row = startRow + 1 ; row <= endRow; row++) {
53
+ result.push_back (array[row][endCol]);
54
+ }
55
+ // traverse the bottom row from right to left
56
+ for (int col = endCol - 1 ; col >= startCol; col--) {
57
+ // check if there is only one row in the subarray
58
+ if (startRow == endRow) {
59
+ break ;
60
+ }
61
+ result.push_back (array[endRow][col]);
62
+ }
63
+ // traverse the left column from bottom to top
64
+ for (int row = endRow - 1 ; row > startRow; row--) {
65
+ // check if there is only one column in the subarray
66
+ if (startCol == endCol) {
67
+ break ;
68
+ }
69
+ result.push_back (array[row][startCol]);
70
+ }
71
+ // update the indices for the next subarray to be traversed
72
+ startRow++;
73
+ endRow--;
74
+ startCol++;
75
+ endCol--;
76
+ }
77
+
78
+ return result;
79
+ }
80
+
81
+ int main () {
82
+ vector<vector<int >> array = {{1 , 2 , 3 }, {4 , 5 , 6 }, {7 , 8 , 9 }};
83
+ vector<int > result = SpiralTraverse (array);
84
+ cout << " Spiral traversal: " ;
85
+ for (int i = 0 ; i < result.size (); i++) {
86
+ cout << result[i] << " " ;
87
+ }
88
+ cout << endl;
89
+ return 0 ;
90
+ }
0 commit comments