-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathspairalOrderMatrix.cpp
More file actions
47 lines (41 loc) · 1.15 KB
/
spairalOrderMatrix.cpp
File metadata and controls
47 lines (41 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;
// Time complexity O(nm)
int main()
{
int n,m;
cout<<"enter no of rows and coulmns"<<endl;
cin>>n>>m;
int arr[n][m];
cout<<"Enter's elements"<<endl;
for(int i=0; i<n; i++){
for(int j =0; j<m; j++){
cin>>arr[i][j];
}
}
cout<<"Spairal oraer matrix.."<<endl;
int rowStart =0, rowEnd=n-1, colStart = 0, colEnd =m-1;
while(rowStart <= rowEnd && colStart <= colEnd) {
// first row left to right
for(int col = colStart; col <= colEnd; col++){
cout<<arr[rowStart][col]<<" ";
}
rowStart++;
// last column downword
for(int row = rowStart; row <= rowEnd; row++){
cout<<arr[row][colEnd]<<" ";
}
colEnd--;
// lart row right to left
for(int col = colEnd; col >= colStart; col--){
cout<<arr[rowEnd][col]<<" ";
}
rowEnd--;
// first column upword
for(int row = rowEnd; row >= rowStart; row--){
cout<<arr[row][colStart]<<" ";
}
colStart++;
}
return 0;
}