Advertisement
unknown_0711

Untitled

Jul 15th, 2022
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main
  6. {
  7.     public static void main (String[] args) throws java.lang.Exception
  8.     {
  9.         //your code here
  10.       Scanner sc = new Scanner(System.in);
  11.         int r = sc.nextInt();
  12.         int c = sc.nextInt();
  13.         int mat[][] =new int[r][c];
  14.         for (int i =0; i<r ; i++){
  15.             for (int j=0; j<c; j++){
  16.                 mat[i][j] = sc.nextInt();
  17.             }
  18.         }
  19.       int top =0;       int bottom = r-1;
  20.       int right= c-1;   int left =0;
  21.       while(top <= bottom && left <=right)
  22.       {
  23.         for(int i= left; i<=right; i++)
  24.           {
  25.             System.out.print(mat[top][i] +" ");
  26.            
  27.           }
  28.         top++;
  29.          for(int i= top; i<=bottom; i++)
  30.           {
  31.             System.out.print(mat[i][right] +" ");
  32.            
  33.           }
  34.         right--;
  35.         if(top<=bottom)
  36.         {
  37.           for(int i=right; i>= left; i--)
  38.             {
  39.                System.out.print(mat[bottom][i]+ " ");
  40.              
  41.             }
  42.           bottom--;
  43.         }
  44.         if(left<=right)
  45.         {
  46.           for(int i=bottom; i>= top; i--)
  47.             {
  48.                System.out.print(mat[i][left] +" ");
  49.              
  50.             }
  51.           left++;
  52.         }
  53.       }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement