-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_BoundaryTraversal.cpp
70 lines (57 loc) · 1.41 KB
/
GFG_BoundaryTraversal.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
66
67
68
69
70
/*
https://practice.geeksforgeeks.org/problems/boundary-elements-of-matrix1102/1/#
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<int> BoundaryElements(vector<vector<int>>&matrix){
// Code here
int r = matrix.size();
int c = matrix[0].size();
vector<int> ans;
//TC: O(n^2)
// for(int i=0; i<r; i++)
// {
// for(int j=0; j<c; j++)
// {
// if(i==0 || j==0 || i==r-1 || j==c-1)
// ans.push_back(matrix[i][j]);
// }
// }
for(int j=0;j<c; j++)
ans.push_back(matrix[0][j]);
for(int i=1; i<r-1; i++)
{
ans.push_back(matrix[i][0]);
ans.push_back(matrix[i][c-1]);
}
if(r>1)
for(int j=0; j<c; j++)
{
ans.push_back(matrix[r-1][j]);
}
return ans;
}//end
};
// { Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int n;
cin >> n;
vector<vector<int>>matrix(n, vector<int>(n, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> matrix[i][j];
Solution obj;
vector<int>ans = obj.BoundaryElements(matrix);
for(auto i: ans)
cout << i << " ";
cout << "\n";
}
return 0;
} // } Driver Code Ends