Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- //your code here
- Scanner sc = new Scanner(System.in);
- int r = sc.nextInt();
- int c = sc.nextInt();
- int mat[][] =new int[r][c];
- for (int i =0; i<r ; i++){
- for (int j=0; j<c; j++){
- mat[i][j] = sc.nextInt();
- }
- }
- int top =0; int bottom = r-1;
- int right= c-1; int left =0;
- while(top <= bottom && left <=right)
- {
- for(int i= left; i<=right; i++)
- {
- System.out.print(mat[top][i] +" ");
- }
- top++;
- for(int i= top; i<=bottom; i++)
- {
- System.out.print(mat[i][right] +" ");
- }
- right--;
- if(top<=bottom)
- {
- for(int i=right; i>= left; i--)
- {
- System.out.print(mat[bottom][i]+ " ");
- }
- bottom--;
- }
- if(left<=right)
- {
- for(int i=bottom; i>= top; i--)
- {
- System.out.print(mat[i][left] +" ");
- }
- left++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement