This is my JAVA code for this question-
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner ip=new Scanner(System.in);
int n=ip.nextInt();
int arr[][]=new int[n+1][n+1];
int a=1;
int i=1;
int j=1;
int k=1;
int z=0;
while(1>0) {
int m=1;
if(k==n*2) {
break;
}
else if(k>n) {
i=k-n+1;
j=n;
z--;
}else {
i=1;
j=k;
z++;
}
while(m<=z) {
arr[i][j]=a;
a++;
m++;
i++;
j--;
}
k++;
}
for(int r=1;r<=n;r++) {
for(int c=1;c<=n;c++) {
System.out.printf("%5d ",arr[r][c]);
}
System.out.println();
}
}
}
and the pattern printing for different cases is also correct,help me finding what is wrong in this.
Pattern for n=4:
1 2 4 7
3 5 8 11
6 9 12 14
10 13 15 16