-
Notifications
You must be signed in to change notification settings - Fork 109
/
MaximumPrimeInDiagMatrix.java
76 lines (72 loc) · 1.88 KB
/
MaximumPrimeInDiagMatrix.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* 12 4 8
5 7 6
4 8 13
Maxumum prime of the 2 diagonals 13
*/
import java.util.*;
public class MaximumPrimeInDiagMatrix
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int i,j;
System.out.print("Enter number of row/column :");
int n = sc.nextInt();
int[][] arr = new int[n][n];
int[] dia1 = new int[n];
int[] dia2 = new int[n];
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
arr[i][j]=sc.nextInt();
if(i==j)
{
dia1[i]=arr[i][j];
}
}
}
for(i=0; i<n; i++)
{
for(j=n-1; j>=0; j--)
{
if(i == (n-1-j))
{
dia2[i]=arr[i][j];
}
}
}
System.out.println(Arrays.toString(dia2));
int[] result = new int[dia1.length+dia2.length];
System.arraycopy(dia1,0,result,0,dia1.length);
System.arraycopy(dia2, 0, result, dia1.length, dia2.length);
System.out.println(Arrays.toString(result));
int[] prime = new int[0];
for(i=0; i<result.length; i++)
{
int check =0;
for(j=2; j<result[i]; j++)
{
if(result[i]%j==0)
{
check++;
}
}
if(check==0)
{
prime = Arrays.copyOf(prime,prime.length+1);
prime[prime.length-1]=result[i];
}
}
System.out.println(Arrays.toString(prime));
int max = 0;
for (i=0; i<prime.length; i++)
{
if(prime[i]>max)
{
max = prime[i];
}
}
System.out.println("The maximum prime : "+max);
}
}