11// Printing Matrix in spiral order
22#include < bits/stdc++.h>
33using 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]);
1020 }
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]);
1524 }
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 ;
2130 }
22- endRow-- ;
31+ result. push_back (array[ endRow][col]) ;
2332 }
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 ;
2838 }
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]);
3940 }
41+ // update the indices for the next subarray to be traversed
42+ startRow++;
43+ endRow--;
44+ startCol++;
45+ endCol--;
4046 }
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] << " " ;
4657 }
47- print_spiral (Mat, R, C) ;
58+ cout << endl ;
4859 return 0 ;
4960}
0 commit comments