-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOTD-Boundary-traversal-of-matrix.cpp
65 lines (56 loc) · 1.46 KB
/
POTD-Boundary-traversal-of-matrix.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to return list of integers that form the boundary
//traversal of the matrix in a clockwise manner.
vector<int> boundaryTraversal(vector<vector<int> > matrix, int n, int m)
{
vector<int> ans;
if(n==1)
{
for(int i=0;i<m;i++) ans.push_back(matrix[0][i]);
}
else if(m==1)
{
for(int i=0;i<n;i++) ans.push_back(matrix[i][0]);
}
else
{
for(int i=0;i<m-1;i++) ans.push_back(matrix[0][i]);
for(int i=0;i<n-1;i++) ans.push_back(matrix[i][m-1]);
for(int i=m-1;i>0;i--) ans.push_back(matrix[n-1][i]);
for(int i=n-1;i>0;i--) ans.push_back(matrix[i][0]);
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
vector<vector<int> > matrix(n);
for(int i=0; i<n; i++)
{
matrix[i].assign(m, 0);
for( int j=0; j<m; j++)
{
cin>>matrix[i][j];
}
}
Solution ob;
vector<int> result = ob.boundaryTraversal(matrix, n, m);
for (int i = 0; i < result.size(); ++i)
cout<<result[i]<<" ";
cout<<endl;
}
return 0;
}
// } Driver Code Ends