Abhimanyu's Matrix Traversal

 Coding Problem Keys 

 Abhimanyu's Matrix Traversal 

 Problem Statement 

Given an integer matrix, R x C, traverse it in the order as shown below:


 Input Format 

One line containing two integers R and C representing the dimensions of the matrix M as rows R, and columns C, respectively.
R lines, each containing C space separated number of integers which collectively form the matrix data.

 Output Format 

Single line containing integers, representing the desired traversal.

 Constraints 

0 < R < 50
0 < C < 50

 Sample Input/Output 1 

 Input 

1 1
1

 Output 

1

 Sample Input/Output 2 

 Input 

2 2
1 2
1 2

 Output 

1 2 2 1

 Max Execution Time Limit: 5000 millisecs 

 Solution 

 Programming Language: C++ Language 

#include<iostream>
using namespace std;
int main(){
    int m,n;
    cin>>m>>n;
    int mat[m][n];
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            cin>>mat[i][j];
        }
    }
    int k=0,l=0;
    {
        int last_row=m-1,last_col=n-1;
        while(k<=last_row && l<=last_col){
            for(int i=l;i<=last_col;i++){
                cout<<mat[k][i]<<" ";
            }
            k++;
            for(int i=k;i<=last_row;i++){
                cout<<mat[i][last_col]<<" ";
            }
            last_col--;
            if(k<=last_col){
                for(int i=last_col;i>=l;i--){
                    cout<<mat[last_row][i]<<" ";
                }
                last_row--;
            }
            if(l<=last_col){
                for(int i=last_row;i>=k;i--){
                    cout<<mat[i][l]<<" ";
                }
                l++;
            }
        }
    }
}

// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Maximum Goods Transported

Desktop Products

Matrix Print Excluding Border Elements

Words Starting with Upper Case

Find Maxima/Minima

Swap and Reverse the String

Flowchart Logic 047