My answer is correct although i dont understand why it is not submitting

here is my code

import java.util.*;
public class Main {
public static void main(String args[]) {
int n,m;
Scanner in=new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
int arr[][]=new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
arr[i][j]=in.nextInt();
}
}
int rowstr=0;
int rowend=n;
int colstr=0;
int colend=m;
int i;

    while(rowstr < rowend && colstr < colend)
    {
        for( i=colstr;i<colend;i++)
        {
            System.out.print(arr[rowstr][i]+", ");
        }
        rowstr++;
        for (i = rowstr; i < rowend; i++)
        { 
            System.out.print(arr[i][rowend - 1] + ", "); 
        } 
        colend--; 
         if (rowstr < rowend) { 
        for (i = colend - 1; i >= colstr; i--)
            { 
                System.out.print(arr[rowend - 1][i] + ", "); 
            } 
        rowend--;
         }
        if (colstr < colend) { 
            for (i = rowend - 1; i >= rowstr; --i) { 
                System.out.print(arr[i][colstr] + ", "); 
            } 
            colstr++;
        }
    }
    System.out.print("END"); 
}

}

Hi Sankalp,
Your code is not correct. There’s a very trivial mistake. Your code will work for square matrices (eg 3x3) but will fail for matrices where column!=row. In your second for loop :
for (i = rowstr; i < rowend; i++)
{
System.out.print(arr[i][rowend - 1] + ", ");
}
change arr[i][rowend-1] to arr[i][colend-1]. :slight_smile: