-
Notifications
You must be signed in to change notification settings - Fork 243
/
numberDecrement.java
52 lines (42 loc) · 1.11 KB
/
numberDecrement.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
// Input Format: N = 6
// Result:
// 6 6 6 6 6 6 6 6 6 6 6
// 6 5 5 5 5 5 5 5 5 5 6
// 6 5 4 4 4 4 4 4 4 5 6
// 6 5 4 3 3 3 3 3 4 5 6
// 6 5 4 3 2 2 2 3 4 5 6
// 6 5 4 3 2 1 2 3 4 5 6
// 6 5 4 3 2 2 2 3 4 5 6
// 6 5 4 3 3 3 3 3 4 5 6
// 6 5 4 4 4 4 4 4 4 5 6
// 6 5 5 5 5 5 5 5 5 5 6
// 6 6 6 6 6 6 6 6 6 6 6
// Input Format: N = 3
// Result:
// 3 3 3 3 3
// 3 2 2 2 3
// 3 2 1 2 3
// 3 2 2 2 3
// 3 3 3 3 3
class Main {
static void pattern(int n)
{
// Outer loop for no. of rows
for(int i=0;i<2*n-1;i++){
// inner loop for no. of columns.
for(int j=0;j<2*n-1;j++){
// Initialising the top, down, left and right indices of a cell.
int top = i;
int bottom = j;
int right = (2*n - 2) - j;
int left = (2*n - 2) - i;
System.out.print(n- Math.min(Math.min(top,bottom), Math.min(left,right)) + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int N = 6;
pattern(N);
}
}