MATRIX
Find some Simple and complex logic problems with matrix
Find some Simple and complex logic problems with matrix
Spiral matrix printing
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();
}
}
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 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output 1:
Spiral Matrix :
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
input 2:
6 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
output 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 5
Find string row-wise in a matrix
import 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:
6
l w r w s g
s k r l l r
k s k i l l
r q e g h u
r t k e w c
q s k i l l
skill
output 1:
3
6
input 2:
4
s k i l
k t h e
r t c k
k t h e
the
Output 2:
2
4
Find String Column wise in a matrix
import 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:
6
l w y w k g
s k s l k r
k s k i s l
i q i g p u
l t l e l c
l s l i l l
skill
output 1:
1
3
Sort 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 3
18 91 8
45 8 10
9 14 7
output 1:
8 18 91
8 10 45
7 9 14
Sort 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 3
1 1 1
0 1 0
0 1 0
0 0 1
1 0 0
output 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.
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 3
1 1 1
0 1 0
0 1 0
0 0 1
1 0 0
output 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:
4
Enter column:
6
10 10 2 0 20 4
1 0 0 30 2 5
0 10 4 0 2 0
1 0 2 20 0 4
output:
74
explanation:
the path with maximum sum is 20->30->4->20
Matrix 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 3
2 3 3
2 3 3
2 3 3
2
output 1:
yes
S 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 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
output 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 25
Matrix 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:
4
4 2 6 8
5 7 9 6
1 2 4 2
9 10 5 7
output 1:
YES
input 2:
6
1 3 4 6 7 2
1 3 5 8 1 2
4 2 10 4 9 2
8 9 4 5 7 2
1 8 3 2 0 1
9 6 7 2 3 5
output 2:
NO
Quadrant 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 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output 1:
-1
explanation:
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 6
1 3 5 1 3 0
1 19 3 1 47 1
7 35 5 29 9 2
18 57 39 29 9 4
27 58 2 5 2 6
84 6 2 7 48 91
output 2:
1 3 5
1 19 3
7 35 5
Row 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 4
1 2 3 4
5 6 0 8
0 10 11 12
13 14 15 16
output 2:
0 2 0 4
0 0 0 0
0 0 0 0
0 14 0 16
Rotate 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 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output 1:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
input 2:
2 3
1 4 9
5 8 6
output 2:
5 1
8 4
6 9
Matrix 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 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output 1:
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
input 2:
2 3
1 4 9
5 8 6
output 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:
3
1 2 3
4 5 6
7 8 9
0
output 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:
4
1 56 28 9
17 38 18 49
1 5 2 9
27 59 10 45
100
output 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 wise
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 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output 1:
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
input 2:
4 2
1 2
3 4
5 6
7 8
output 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 5
1 0 0 1 0
1 1 1 1 0
1 0 0 1 1
0 0 1 0 0
output 2:
4
All 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 9
9 9 9
9 5 9
9 9 9
output 1:
YES
input 2:
3 3 7
7 7 5
5 9 7
7 5 5
output 2:
NO
Integer 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:
2586
output 1:
22558866
22558866
input 2:
37895
output 2:
333777888999555
333777888999555
333777888999555
input 3:
9688323
output 3:
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
999999999666666666888888888888888888333333333222222222333333333
Spiral 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 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output 1:
Spiral Matrix :
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
input 2:
6 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
output 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 5
Spiral 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 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output 1:
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
input 2:
4 2
1 2
3 4
5 6
7 8
output 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 5
4 2 9 6 1
7 9 6 5 4
5 7 3 8 8
7 4 9 9 4
output 1:
53
Collect 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 5
4 2 9 6 1
7 9 6 5 4
5 7 3 8 8
7 4 9 9 4
0 1
output 1:
44