Spiral matrix printingimport java.util.*;public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int mat[][]=new int[R][C]; for(int row = 0; row < R; row++) { for(int col = 0; col < C; col++) { mat[row][col]=sc.nextInt(); } } System.out.println("Spiral Matrix : "); int start = 0, end = 0; while(start < R && end < C) { for(int col = end; col < C; col++) { System.out.print(mat[start][col]+" "); } start++; for(int row = start; row < R; row++) { System.out.print(mat[row][C-1]+" "); } C--; if(start < R) { for(int col = C - 1; col >= end; --col) { System.out.print(mat[R-1][col]+" "); } R--; } if(end < C) { for(int row = R - 1; row >= start; row--) { System.out.print(mat[row][end]+" "); } end++; } } }}input 1:4 41 2 3 45 6 7 89 10 11 1213 14 15 16output 1:Spiral Matrix : 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 input 2:6 51 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 30output 2:Spiral Matrix : 1 2 3 4 5 10 15 20 25 30 29 28 27 26 21 16 11 6 7 8 9 14 19 24 23 22 17 12 13 18 input 3:3 3 1 2 3 4 5 6 7 8 9 output 3:Spiral Matrix : 1 2 3 6 9 8 7 4 5Find string row-wise in a matriximport java.util.*;public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); char[][] mat=new char[N+1][N+1]; for(int row=1;row<=N;row++) { for(int col=1;col<=N;col++) { mat[row][col]=sc.next().charAt(0); } } String str=sc.next(); int l=str.length(); int count=0,index=0; for(int row=1;row<=N;row++) { count=0; index=0; for(int col=1;col<=N;col++) { if(mat[row][col]==str.charAt(index)) { count++; index++; } } if(count==l) { System.out.println(row); } } }}input 1:6l w r w s gs k r l l rk s k i l lr q e g h ur t k e w cq s k i l lskilloutput 1:36input 2:4s k i lk t h er t c kk t h etheOutput 2:24Find String Column wise in a matriximport java.util.*;public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); char[][] mat=new char[N+1][N+1]; for(int row=1;row<=N;row++) { for(int col=1;col<=N;col++) { mat[row][col]=sc.next().charAt(0); } } String str=sc.next(); int l=str.length(); int count=0,index=0; for(int col=1;col<=N;col++) { count=0; index=0; for(int row=1;row<=N;row++) { if(mat[row][col]==str.charAt(index)) { count++; index++; } } if(count==l) { System.out.println(col); } } }}input 1:6l w y w k gs k s l k rk s k i s li q i g p ul t l e l cl s l i l lskilloutput 1:1 3Sort matrix row-wise:import java.util.*;public class MyClass{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } for(int row=0;row<R;row++) { Arrays.sort(mat[row]); } for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { System.out.print(mat[row][col]+" "); } System.out.println(); } }}input 1:3 318 91 845 8 109 14 7output 1:8 18 91 8 10 45 7 9 14Sort the rows based on its equivalent binary values (METHOD 1):import java.util.*;public class Hello{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int mat[][]=new int[R][C]; long binarr[]=new long[R]; for(int row=0;row<R;row++) { String str=""; for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); str+=mat[row][col]; } Long x=Long.parseLong(str,2); binarr[row]=(x*100)+row; } Arrays.sort(binarr); for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { long x=(long)binarr[row]%100; System.out.print(mat[(int)x][col]+" "); } System.out.println(); } }}input 1:5 31 1 10 1 00 1 00 0 11 0 0output 1:0 0 1 0 1 0 0 1 0 1 0 0 1 1 1explanation:the binary to decimal value row-wise is 7,2,2,1,4 on sorting this, and sorting the corresponsind rows, the final matrix is printed.Sort the rows based on its equivalent binary values (METHOD 2):import java.util.*;public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); List<String> al=new ArrayList<>(); int index=0; for(int index1=1;index1<=R+1;index1++) { String str=sc.nextLine(); String[] arr=str.split(" "); String str1=""; for(int index2=0;index2<arr.length;index2++) { str1=str1+arr[index2]; } al.add(str1); } Collections.sort(al); for(int index1=1;index1<=R;index1++) { String temp=al.get(index1); for(int index2=0;index2<temp.length();index2++) { System.out.print(temp.charAt(index2)+" "); } System.out.println(); } }}input 1:5 31 1 10 1 00 1 00 0 11 0 0output 1:0 0 1 0 1 0 0 1 0 1 0 0 1 1 1 explanation:the binary to decimal value row-wise is 7,2,2,1,4 on sorting this, and sorting the corresponsind rows, the final matrix is printed.Path with maximum sum in a matrix:import java.util.*;public class Main{ static int findMaxPath(int mat[][],int R, int C) { int max=-1; for(int col=0;col<C;col++) { max=Math.max(max,mat[0][col]); } for(int row=1;row<R;row++) { max=-1; for(int col=0;col<C;col++) { if(col>0&&col<C-1) { mat[row][col]+=Math.max(mat[row-1][col],Math.max(mat[row-1][col-1],mat[row-1][col+1])); } else if(col>0) { mat[row][col]+=Math.max(mat[row-1][col],mat[row-1][col-1]); } else if(col<C-1) { mat[row][col]+=Math.max(mat[row-1][col],mat[row-1][col+1]); } max=Math.max(max,mat[row][col]); } } return max; } public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter row:"); int R=sc.nextInt(); System.out.println("Enter column:"); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } System.out.println(findMaxPath(mat,R,C)); }}input:Enter row:4Enter column:610 10 2 0 20 41 0 0 30 2 50 10 4 0 2 01 0 2 20 0 4output:74explanation:the path with maximum sum is 20->30->4->20Matrix containing atleast on KxK submatrix of same elements:import java.util.*;public class Main { static int findMatrix(int[][] mat,int R,int C) { int[][] dp=new int[R][C]; int res=0; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { if(row==0||col==0) { dp[row][col]=1; } else { if(mat[row][col]==mat[row-1][col]&&mat[row][col]==mat[row][col-1]&&mat[row][col]==mat[row-1][col-1]) { dp[row][col]=(dp[row-1][col]>dp[row][col-1]&&dp[row-1][col]>dp[row-1][col-1]+1)?dp[row-1][col]:(dp[row][col-1]>dp[row-1][col]&&dp[row][col-1]>dp[row-1][col-1]+1)?dp[row][col-1]:dp[row-1][col-1]+1; } else { dp[row][col]=1; } } res=res>dp[row][col]?res:dp[row][col]; } } return res; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int k=sc.nextInt(); int num=findMatrix(mat,R,C); if(num==k) System.out.print("yes"); else System.out.print("no"); }}input 1:3 32 3 32 3 32 3 32output 1:yesS shaped traversal from top to bottom:
import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R+1][C+1]; for(int row=1;row<=R;row++) { for(int col=1;col<=C;col++) { mat[row][col]=sc.nextInt(); } } for(int row=1;row<=R;row++) { if(row%2==1) { for(int col=1;col<=C;col++) { System.out.print(mat[row][col]+" "); } } else if(row%2==0) { for(int col=C;col>0;col--) { System.out.print(mat[row][col]+" "); } } } }}input 1:5 51 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25output 1:1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 25Matrix Containing atleast one repeating 2X2 submatrix:import java.util.*;public class Main{ public static boolean findMatrix(int[][] mat,int currRow,int currCol,int N) { for(int row=0;row<N-1;row++) { for(int col=0;col<N-1;col++) { if(Math.abs(currRow-row)>1 || Math.abs(currCol-col)>1) { if(mat[currRow][currCol]==mat[row][col] && mat[currRow][currCol+1]==mat[row][col+1] && mat[currRow+1][currCol]==mat[row+1][col] && mat[currRow+1][currCol+1]==mat[row+1][col+1]) return true; } } } return false; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); int[][] mat=new int[N][N]; for(int row=0;row<N;row++) { for(int col=0;col<N;col++) { mat[row][col]=sc.nextInt(); } } for(int row=0;row<N-1;row++) { for(int col=0;col<N-1;col++) { if(findMatrix(mat,row,col,N)) { System.out.print("YES"); return; } } } System.out.print("NO"); }}input 1:44 2 6 85 7 9 61 2 4 29 10 5 7output 1:YESinput 2:61 3 4 6 7 21 3 5 8 1 24 2 10 4 9 28 9 4 5 7 21 8 3 2 0 19 6 7 2 3 5output 2:NOQuadrant with Maximum odd integers:import java.util.*;public class Main{ static void print(int[][] mat,int startR,int startC,int endR,int endC) { for(int row=startR;row<endR;row++) { for(int col=startC;col<endC;col++) { System.out.print(mat[row][col]+" "); } System.out.println(); } } static int check(int[][] mat,int startR,int startC,int endR,int endC) { int count=0; for(int row=startR;row<endR;row++) { for(int col=startC;col<endC;col++) { if(mat[row][col]%2==1) { count++; } } } return count; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); List<Integer> al=new ArrayList<>(); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int startR=0,startC=0,endR=R-1,endC=C-1,count=0; for(int index=1;index<=4;index++) { count=0; if(index==1) { count=check(mat,0,0,R/2,C/2); } else if(index==2) { count=check(mat,0,C/2,R/2,C); } else if(index==3) { count=check(mat,R/2,0,R,C/2); } else if(index==4) { count=check(mat,R/2,C/2,R,C); } al.add(count); } int max=0,j=0; for(int i=0;i<al.size();i++) { if(al.get(i)>max) { max=al.get(i); j=i; } } int ctr=0; for(int i=0;i<al.size();i++) { if(al.get(i)==max) ctr++; } if(ctr>1) { System.out.print("-1"); System.exit(0); } else { int q=j+1; if(q==1) { print(mat,0,0,R/2,C/2); } else if(q==2) { print(mat,0,C/2,R/2,C); } else if(q==3) { print(mat,R/2,0,R,C/2); } else if(q==4) { print(mat,R/2,C/2,R,C); } } }}input 1:4 41 2 3 4 5 6 7 89 10 11 1213 14 15 16output 1:-1explanation:the quadrant with maximum odd integers should be printed. if there is more than one quadrant with max odd integers, the program must print "-1".input 2:6 61 3 5 1 3 01 19 3 1 47 17 35 5 29 9 218 57 39 29 9 427 58 2 5 2 684 6 2 7 48 91output 2:1 3 5 1 19 3 7 35 5Row and Column - make it ZERO:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); List<Integer> al=new ArrayList<>(); int flag=0; int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); if(mat[row][col]==0) { al.add(row); al.add(col); flag=1; } } } int index=0; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { if(flag!=0) { if(row==al.get(index)&&col==al.get(index+1)) { for(int j=0;j<C;j++) { mat[row][j]=0; } for(int i=0;i<R;i++) { mat[i][col]=0; } if(index+2<al.size()) index+=2; } } } } for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { System.out.print(mat[row][col]+" "); } System.out.println(); } }}input 1:4 41 2 3 45 6 0 8 0 10 11 1213 14 15 16output 2:0 2 0 4 0 0 0 0 0 0 0 0 0 14 0 16Rotate matrix clockwise 90 degree:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int[][] ans=new int[C][R]; int row1=0,col1=0; for(int col=0;col<C;col++) { col1=0; for(int row=R-1;row>=0;row--) { ans[row1][col1++]=mat[row][col]; } row1++; } for(int row=0;row<C;row++) { for(int col=0;col<R;col++) { System.out.print(ans[row][col]+" "); } System.out.println(); } }}input 1:4 41 2 3 4 5 6 7 89 10 11 1213 14 15 16output 1:13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4 input 2:2 31 4 95 8 6output 2:5 1 8 4 6 9Matrix rotation - 90 degree anti clockwise direction:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int[][] ans=new int[C][R]; int row1=0,col1=0; for(int col=C-1;col>=0;col--) { col1=0; for(int row=0;row<R;row++) { ans[row1][col1++]=mat[row][col]; } row1++; } for(int row=0;row<C;row++) { for(int col=0;col<R;col++) { System.out.print(ans[row][col]+" "); } System.out.println(); } }}input 1:4 41 2 3 4 5 6 7 89 10 11 1213 14 15 16output 1:4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13 input 2:2 31 4 95 8 6output 2:9 6 4 8 1 5 Matrix elements surrounded by K:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int[][] mat=new int[R][R]; for(int row=0;row<R;row++) { for(int col=0;col<R;col++) { mat[row][col]=sc.nextInt(); } } int k=sc.nextInt(); int size=(R*2)+1; int row1=0,col1=0; for(int row=1;row<=size;row++) { for(int col=1;col<=size;col++) { if(row%2==0&&col%2==0) { System.out.print(mat[row1][col1]+" "); if(col1==R-1) { row1++; col1=0; } else { col1++; } } else System.out.print(k+" "); } System.out.println(); } }}input 1:31 2 34 5 67 8 90output 1:0 0 0 0 0 0 0 0 1 0 2 0 3 0 0 0 0 0 0 0 0 0 4 0 5 0 6 0 0 0 0 0 0 0 0 0 7 0 8 0 9 0 0 0 0 0 0 0 0 input 2:41 56 28 917 38 18 491 5 2 927 59 10 45100output 2:100 100 100 100 100 100 100 100 100 100 1 100 56 100 28 100 9 100 100 100 100 100 100 100 100 100 100 100 17 100 38 100 18 100 49 100 100 100 100 100 100 100 100 100 100 100 1 100 5 100 2 100 9 100 100 100 100 100 100 100 100 100 100 100 27 100 59 100 10 100 45 100 100 100 100 100 100 100 100 100 100 Spiral matrix printing - Counter Clock wiseimport java.util.*;public class Main{ static void printLeftToRight(int[][] mat,int row,int startCol,int endCol) { for(int col=startCol;col<=endCol;col++) { System.out.print(mat[row][col]+" "); } } static void printRightToLeft(int[][] mat,int row,int startCol,int endCol) { for(int col=endCol;col>=startCol;col--) { System.out.print(mat[row][col]+" "); } } static void printTopToBottom(int[][] mat,int col,int startRow,int endRow) { for(int row=startRow;row<=endRow;row++) { System.out.print(mat[row][col]+" "); } } static void printBottomToTop(int[][] mat,int col,int startRow,int endRow) { for(int row=endRow;row>=startRow;row--) { System.out.print(mat[row][col]+" "); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int Row1=0,Row2=R-1,Col1=0,Col2=C-1; while(Row1<=Row2&&Col1<=Col2) { printTopToBottom(mat,Col1,Row1,Row2); printLeftToRight(mat,Row2,Col1+1,Col2); if(Col1!=Col2) printBottomToTop(mat,Col2,Row1,Row2-1); if(Row1!=Row2) printRightToLeft(mat,Row1,Col1+1,Col2-1); Row1++; Row2--; Col1++; Col2--; } }}input 1:4 41 2 3 45 6 7 89 10 11 1213 14 15 16output 1:1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7input 2:4 21 23 4 5 67 8output 2:1 3 5 7 8 6 4 2 Maximum number of 1's in a row:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int max=Integer.MIN_VALUE,count=0; int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { count=0; for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); if(mat[row][col]==1) count++; } if(count>=max) { max=count; } } System.out.print(max); }}input 1:6 3 1 0 1 1 0 0 1 1 1 1 0 1 1 1 0 0 0 0 output 1:3 input 2:4 51 0 0 1 01 1 1 1 01 0 0 1 10 0 1 0 0output 2:4All the boundary integers same as K:import java.util.*;import java.util.stream.*;public class Main{ static boolean findBoundryIntegers(int[][] mat,int R,int C,int K) { Set<Integer> st=new HashSet<>(); int col1=0,col2=C-1,row1=0,row2=R-1; for(int row=0;row<R;row++) { st.add(mat[row][col1]); st.add(mat[row][col2]); } for(int col=1;col<C-1;col++) { st.add(mat[row1][col]); st.add(mat[row2][col]); } if(st.size()==1&&(st.stream().collect(Collectors.toList()).get(0)==K)) return true; else return false; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int K=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } System.out.print(findBoundryIntegers(mat,R,C,K)?"YES":"NO"); }}input 1:3 3 99 9 99 5 99 9 9output 1:YESinput 2:3 3 77 7 55 9 77 5 5output 2:NOInteger Matrix Pattern Printing:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int l=str.length(); int size=Character.getNumericValue(str.charAt(0)); for(int row=1;row<=size;row++) { for(int col=0;col<l;col++) { for(int k=0;k<size;k++) { System.out.print(str.charAt(col)); } } System.out.println(); } }}input 1:2586output 1:2255886622558866input 2:37895output 2:333777888999555333777888999555333777888999555input 3:9688323output 3:999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333999999999666666666888888888888888888333333333222222222333333333Spiral printing clockwise:import java.util.*;public class Main{ public static void printLeftToRight(int [][]mat,int row,int startCol,int endCol) { for(int col=startCol;col<=endCol;col++) { System.out.print(mat[row][col]+" "); } } public static void printTopToBottom(int [][]mat,int col,int startRow,int endRow) { for(int row=startRow;row<=endRow;row++) { System.out.print(mat[row][col]+" "); } } public static void printRightToLeft(int [][]mat,int row,int startCol,int endCol) { for(int col=endCol;col>=startCol;col--) { System.out.print(mat[row][col]+" "); } } public static void printBottomToTop(int [][]mat,int col,int startRow,int endRow) { for(int row=endRow;row>=startRow;row--) { System.out.print(mat[row][col]+" "); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(),C=sc.nextInt(); int [][]mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int topRow=0,bottomRow=R-1,leftCol=0,rightCol=C-1; while(topRow<=bottomRow&&leftCol<=rightCol) { printLeftToRight(mat,topRow,leftCol,rightCol); printTopToBottom(mat,rightCol,topRow+1,bottomRow); if(topRow!=bottomRow) printRightToLeft(mat,bottomRow,leftCol,rightCol-1); if(leftCol!=rightCol) printBottomToTop(mat,leftCol,topRow+1,bottomRow-1); topRow++; bottomRow--; leftCol++; rightCol--; } } }input 1:4 41 2 3 45 6 7 89 10 11 1213 14 15 16output 1:Spiral Matrix : 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 input 2:6 51 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 30output 2:Spiral Matrix : 1 2 3 4 5 10 15 20 25 30 29 28 27 26 21 16 11 6 7 8 9 14 19 24 23 22 17 12 13 18 input 3:3 3 1 2 3 4 5 6 7 8 9 output 3:Spiral Matrix : 1 2 3 6 9 8 7 4 5Spiral printing-anticlockwise:import java.util.*;public class Main{ static void printLeftToRight(int[][] mat,int row,int startCol,int endCol) { for(int col=startCol;col<=endCol;col++) { System.out.print(mat[row][col]+" "); } } static void printRightToLeft(int[][] mat,int row,int startCol,int endCol) { for(int col=endCol;col>=startCol;col--) { System.out.print(mat[row][col]+" "); } } static void printTopToBottom(int[][] mat,int col,int startRow,int endRow) { for(int row=startRow;row<=endRow;row++) { System.out.print(mat[row][col]+" "); } } static void printBottomToTop(int[][] mat,int col,int startRow,int endRow) { for(int row=endRow;row>=startRow;row--) { System.out.print(mat[row][col]+" "); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int Row1=0,Row2=R-1,Col1=0,Col2=C-1; while(Row1<=Row2&&Col1<=Col2) { printTopToBottom(mat,Col1,Row1,Row2); printLeftToRight(mat,Row2,Col1+1,Col2); if(Col1!=Col2) printBottomToTop(mat,Col2,Row1,Row2-1); if(Row1!=Row2) printRightToLeft(mat,Row1,Col1+1,Col2-1); Row1++; Row2--; Col1++; Col2--; } }}input 1:4 41 2 3 45 6 7 89 10 11 1213 14 15 16output 1:1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7input 2:4 21 23 4 5 67 8output 2:1 3 5 7 8 6 4 2 Collect Maximum points in a matrix:import java.util.*;public class Main { static int findMaxPoints(int[][] mat,int R,int C) { int[][] points=new int[R][C]; points[0][0]=mat[0][0]; for(int row=1;row<R;row++) { points[row][0]=points[row-1][0]+mat[row][0]; } for(int col=1;col<C;col++) { points[0][col]=points[0][col-1]+mat[0][col]; } for(int row=1;row<R;row++) { for(int col=1;col<C;col++) { points[row][col]=mat[row][col]+Math.max(points[row][col-1],points[row-1][col]); } } return points[R-1][C-1]; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } System.out.print(findMaxPoints(mat,R,C)); }}input 1:4 54 2 9 6 17 9 6 5 45 7 3 8 87 4 9 9 4output 1:53Collect Maximum points from starting point till end:import java.util.*;public class Main{ static int findMaxPoints(int[][] mat,int R,int C,int startRow,int startCol) { int[][] points=new int[R][C]; points[startRow][startCol]=mat[startRow][startCol]; for(int row=startRow+1;row<R;row++) { points[row][startCol]=points[row-1][startCol]+mat[row][startCol]; } for(int col=startCol+1;col<C;col++) { points[startRow][col]=points[startRow][col-1]+mat[startRow][col]; } for(int row=startRow+1;row<R;row++) { for(int col=startCol+1;col<C;col++) { points[row][col]=mat[row][col]+Math.max(points[row][col-1],points[row-1][col]); } } return points[R-1][C-1]; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int R=sc.nextInt(); int C=sc.nextInt(); int[][] mat=new int[R][C]; for(int row=0;row<R;row++) { for(int col=0;col<C;col++) { mat[row][col]=sc.nextInt(); } } int startRow=sc.nextInt(); int startCol=sc.nextInt(); System.out.print(findMaxPoints(mat,R,C,startRow,startCol)); }}input 1:4 54 2 9 6 17 9 6 5 45 7 3 8 87 4 9 9 40 1output 1:44