1
1
// Printing Matrix in spiral order
2
2
#include < bits/stdc++.h>
3
3
using namespace std ;
4
- void print_spiral (int Mat[][10 ], int R, int C){
5
- int startRow = 0 , endRow = R-1 , startCol = 0 , endCol = C-1 ;
6
- while (startRow <= endRow && startCol <= endCol){
7
- // Print first row
8
- for (int i = startCol; i <= endCol; i++){
9
- cout << Mat[startRow][i];
4
+
5
+ #include < vector>
6
+ using namespace std ;
7
+
8
+ vector<int > SpiralTraverse (vector<vector<int >> array) {
9
+ vector<int > result; // vector to store the spiral traversal
10
+ int rows = array.size (); // number of rows in the input array
11
+ int cols = array[0 ].size (); // number of columns in the input array
12
+ int startRow = 0 , endRow = rows - 1 ; // indices for the start and end row of the current subarray
13
+ int startCol = 0 , endCol = cols - 1 ; // indices for the start and end column of the current subarray
14
+
15
+ // loop until the entire input array is traversed
16
+ while (startRow <= endRow && startCol <= endCol) {
17
+ // traverse the top row from left to right
18
+ for (int col = startCol; col <= endCol; col++) {
19
+ result.push_back (array[startRow][col]);
10
20
}
11
- startRow++;
12
- // print end col
13
- for (int i = startRow; i <= endRow; i++){
14
- cout << Mat[i][endCol];
21
+ // traverse the right column from top to bottom
22
+ for (int row = startRow + 1 ; row <= endRow; row++) {
23
+ result.push_back (array[row][endCol]);
15
24
}
16
- endCol--;
17
- // Print end row
18
- if (endRow > startRow){
19
- for ( int i = endCol; i >= startCol; i--) {
20
- cout << Mat[endRow][i] ;
25
+ // traverse the bottom row from right to left
26
+ for ( int col = endCol - 1 ; col >= startCol; col--) {
27
+ // check if there is only one row in the subarray
28
+ if (startRow == endRow) {
29
+ break ;
21
30
}
22
- endRow-- ;
31
+ result. push_back (array[ endRow][col]) ;
23
32
}
24
- // print start row
25
- if (startCol < endCol){
26
- for (int i = endRow; i >= startRow; i--){
27
- cout << Mat[i][startCol];
33
+ // traverse the left column from bottom to top
34
+ for (int row = endRow - 1 ; row > startRow; row--) {
35
+ // check if there is only one column in the subarray
36
+ if (startCol == endCol) {
37
+ break ;
28
38
}
29
- startCol++;
30
- }
31
- }
32
- }
33
- int main (){
34
- int Mat[10 ][10 ], R, C;
35
- cin >> R >> C;
36
- for (int i = 0 ; i < R; i++){
37
- for (int j = 0 ; j < C; j++){
38
- cin >> Mat[i][j];
39
+ result.push_back (array[row][startCol]);
39
40
}
41
+ // update the indices for the next subarray to be traversed
42
+ startRow++;
43
+ endRow--;
44
+ startCol++;
45
+ endCol--;
40
46
}
41
- for (int i = 0 ; i < R; i++){
42
- for (int j = 0 ; j < C; j++){
43
- cout << Mat[i][j] << " " ;
44
- }
45
- cout << endl;
47
+
48
+ return result;
49
+ }
50
+
51
+ int main () {
52
+ vector<vector<int >> array = {{1 , 2 , 3 }, {4 , 5 , 6 }, {7 , 8 , 9 }};
53
+ vector<int > result = SpiralTraverse (array);
54
+ cout << " Spiral traversal: " ;
55
+ for (int i = 0 ; i < result.size (); i++) {
56
+ cout << result[i] << " " ;
46
57
}
47
- print_spiral (Mat, R, C) ;
58
+ cout << endl ;
48
59
return 0 ;
49
60
}
0 commit comments