-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathNotAdjacentNumber.java
56 lines (50 loc) · 1.27 KB
/
NotAdjacentNumber.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
//link to the question
//https://codeforces.com/contest/1520/problem/C
import java.util.*;
import java.io.*;
public class NotAdjacentNumber{
public static void main(String []args) throws IOException{
Scanner sc= new Scanner(System.in);
int loop = sc.nextInt();
for(int ij=0;ij<loop;ij++)
{
int n=sc.nextInt();
if(n==1)
{
System.out.println(1);
continue;
}
if(n==2)
{
System.out.println(-1);
continue;
}
int n2 = n*n;
int a[]=new int[n2+1];
int j=1;
for(int i=1;i<=n*n;i++)
{
if(i%2==0)
{
int store = (n2+1)/2;
a[store+j] = i;
j++;
}
else{
a[j] = i;
}
}
for(int i=1;i<=n2;i++)
{
if(i%n==0)
{
System.out.println(a[i]);
}
else
{
System.out.print(a[i]+" ");
}
}
}
}
}