Boundary traversal of matrix | Practice | GeeksforGeeks

You are given a matrix of dimensions n x m. The task is to perform boundary traversal on the matrix in a clockwise manner. 

Example 1:
Input:
n = 4, m = 4
matrix[][] = {{1, 2, 3, 4},
         {5, 6, 7, 8},
         {9, 10, 11, 12},
         {13, 14, 15,16}}
Output: 1 2 3 4 8 12 16 15 14 13 9 5
Explanation:
The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The boundary traversal is:
1 2 3 4 8 12 16 15 14 13 9 5

Example 2:
Input:
n = 3, m = 4
matrrix[][] = {{12, 11, 10, 9},
         {8, 7, 6, 5},
         {4, 3, 2, 1}}
Output: 12 11 10 9 5 1 2 3 4 8


Your Task:
Complete the function boundaryTraversal() that takes matrix, n and m as input parameters and returns the list of integers that form the boundary traversal of the matrix in a clockwise manner.

Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)

Constraints:
1 <= n, m<= 100
0 <= matrixi <= 1000
0
jerinfrancis1 week ago
vector<int> boundaryTraversal(vector<vector<int> > arr, int n, int m) 
    {
        // code here
        vector<int> r;
        
        int i=0, j=0;
        while(j<=m-1) {
            r.push_back(arr[i][j++]);
        }
        i++;
        if(i==n) return r;
        j--;
        while(i<=n-1) {
            r.push_back(arr[i++][j]);
        }
        j--;
        if(j==-1) return r;
        i--;
        while(j>=0) {
            r.push_back(arr[i][j--]);
        }
        i--;
        j++;
        while(i>=1) {
            r.push_back(arr[i--][j]);
        }
        return r;
    }
0
kuldeepy104591 week ago
 vector<int> boundaryTraversal(vector<vector<int> > matrix, int n, int m)    {       // code here       // starting row       int strow=0,endcol=m-1,endrow=n-1,stcol=0;       int total=0;       if(m==1){           total=n;       }       else if(n==1){           total=m;       }       else{           total=2*(m+n-2);       }      int count=0;       vector<int>ans;       for(int i=0;i<=endcol&&count<total;i++){           ans.push_back(matrix[strow][i]);           count++;                  }       strow++;       // ending col        for(int i=strow;i<=endrow&&count<total;i++){           ans.push_back(matrix[i][endcol]);           count++;       }       endcol--;       // ending row       for(int i=endcol;i>=stcol&&count<total;i--){           ans.push_back(matrix[endrow][i]);           count++;       }       endrow--;       // start col       for(int i=endrow;i>=strow&&count<total;i--){           ans.push_back(matrix[i][stcol]);           count++;                  }       stcol++;       return ans;   }
0
badgujarsachin834 weeks ago
vector<int> boundaryTraversal(vector<vector<int> > matrix, int n, int m) 
    {
        // code here
        vector<int> v;
        if(n==1){
            for(int i=0;i<m;i++){
                v.push_back(matrix[0][i]);
            }
        }else if(m==1){
            for(int i=0;i<n;i++){
                v.push_back(matrix[i][0]);
            }
        }else{
            for(int i=0;i<m;i++){
                v.push_back(matrix[0][i]);
            }
            for(int i=1;i<n;i++){
                v.push_back(matrix[i][m-1]);
            }
            for(int i=m-2;i>=0;i--){
                v.push_back(matrix[n-1][i]);
            }
            for(int i=n-2;i>=1;i--){
                v.push_back(matrix[i][0]);
            }
        }
        return v;
    }
0
rahul8781 month ago
 static ArrayList<Integer> boundaryTraversal(int a[][], int n, int m)
    {
        ArrayList<Integer> l=new ArrayList<>();
        if(n==1 || m==1){
            for(int i=0;i<n;i++){
            
            for(int j=0;j<m;j++){
                
                l.add(a[i][j]);
                
                
            }
        }
        
        }
        else{
        for(int i=0;i<n;i++){
            
            for(int j=0;j<m;j++){
                if(i==0 )
                l.add(a[i][j]);
                if(j==m-1 && i!=0)
                l.add(a[i][j]);
                
            }
        }
        for(int i=n-1;i>=0;i--){
            
            for(int j=m-1;j>=0;j--){
                if(i==n-1 && j<m-1)
                l.add(a[i][j]);
                else if(j==0 && i!=n-1 && i!=0)
                l.add(a[i][j]);
                
            }
        }}
        return l;
    }
-1
amiransarimy1 month ago
Simple Python Solutions
 
def BoundaryTraversal(self,matrix, n, m):
       res =[] 
       if n == 1:
           for j in range(m):
               res.append(matrix[0][j])
       elif m == 1:
           for i in range(n):
               res.append(matrix[i][0])
       
       else:
           for i in range(m):
               res.append(matrix[0][i])
           
           if m > 1:
               for i in range(1,n):
                   res.append(matrix[i][m-1])
           
           if n > 1:
               for i in range(m-2,-1,-1):
                   res.append(matrix[n-1][i])
               
               for i in range(n-2,0,-1):
                   res.append(matrix[i][0])
       
       return res
0
saiakhilpodduturi1 month ago
Boundary traversal of matrix
class Solution
{
    //Function to return list of integers that form the boundary 
    //traversal of the matrix in a clockwise manner.
    static ArrayList<Integer> boundaryTraversal(int matrix[][], int n, int m)
    {
        // code here 
        ArrayList<Integer> res = new ArrayList<>();
        // if matrix has only one row
        if (n == 1)
        {
            for (int i=0; i<m; i++)
                res.add(matrix[0][i]);
            return res;
        }
        // if matrix has only one column
        else if (m == 1)
        {
            for (int i=0; i<n; i++)
                res.add(matrix[i][0]);
            return res;
        }
        // if matrix has more than one row and more than one column
        // first row elements
        for (int i=0; i<m; i++)
        {
            res.add(matrix[0][i]);
        }
        // second to last but one row, corner elements
        for (int i=1; i<n-1; i++)
        {
            res.add(matrix[i][m-1]);
        }
        // last row elements
        for (int i=m-1; i>=0; i--)
        {
            res.add(matrix[n-1][i]);
        }
        // last but one row to second row starting elements
        for (int i=n-2; i>=1; i--)
        {
            res.add(matrix[i][0]);
        }
        return res;
    }
}
-1
pritamaber1 month ago
        vector<int> res;
        
        if(n==1){
            for(int i=0; i<m; i++){
                res.push_back(matrix[0][i]);
            }
        }else if(m==1){
            for(int i=0; i<n; i++){
                res.push_back(matrix[i][0]);
            }
        }else{
            for(int i=0; i<m; i++){
                res.push_back(matrix[0][i]);
            }
            for(int i=1; i<n; i++){
                res.push_back(matrix[i][m-1]);
            }
            for(int i=m-2;i>=0;i--){
                res.push_back(matrix[n-1][i]);
            }
            for(int i=n-2; i>=1;i--){
                res.push_back(matrix[i][0]);
            }   
        }
        return res;
-1
akshatmathur232 months ago
JAVA SOLUTION
class Solution{   //Function to return list of integers that form the boundary    //traversal of the matrix in a clockwise manner.   static ArrayList<Integer> boundaryTraversal(int matrix[][], int n, int m)   {       // code here        ArrayList<Integer> list = new ArrayList<>();       if(matrix == null || n == 0)           return list;       int top = 0, left = 0, bottom = n - 1, right = m - 1;       if(n == 1) {           for(int i = left; i<= right; i++)               list.add(matrix[top][i]);           return list;       }       if(m == 1) {           for(int i = top; i <= bottom; i++)               list.add(matrix[i][right]);           return list;       }       for(int i = left; i <= right; i++)           list.add(matrix[top][i]);       top++;       for(int i = top; i <= bottom; i++)           list.add(matrix[i][right]);       right--;       for(int i = right; i >= left; i--)           list.add(matrix[bottom][i]);       bottom--;           for(int i = bottom; i >= top; i--)           list.add(matrix[i][left]);       left++;       return list;       }} 
-1
subhammahanta183 months ago
//Java Solution
 
class Solution{   //Function to return list of integers that form the boundary    //traversal of the matrix in a clockwise manner.   static ArrayList<Integer> boundaryTraversal(int matrix[][], int n, int m)   {       ArrayList<Integer>ar=new ArrayList<>();       int j;       for(int i=0;i<n;i++)       {        if(i==0 || i==n-1)        {            if(i==0)            {            for(j=0;j<m;j++)             ar.add(matrix[i][j]);            }                  else            {            for(j=m-1;j>=0;j--)            ar.add(matrix[i][j]);                        if(matrix[i].length==1)             continue;                         while((i-1)!=0)            {                i--;                ar.add(matrix[i][0]);            }            break;         }        }        else        {            j=m-1;            ar.add(matrix[i][j]);        }       }       return ar;   }} 
-1
rohithsowmithra3 months ago
python:
=========
def BoundaryTraversal(self,matrix, n, m):       # code here        res = []       for i in range(m):           res.append(matrix[0][i])              if n > 1:           for i in range(1, n):               res.append(matrix[i][m-1])                      if m > 1:               for i in range(m-2,0,-1):                   res.append(matrix[n-1][i])                              for i in range(n-1, 0, -1):                   res.append(matrix[i][0])              return res
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.