I’m always getting test case fail for negative number , even I copied and paste by unlocking editorial , its not submitting the code ,
I want to submit the code for another pattern but not able to do anything.
Here’s my code for Pattern InvertedHourGlass , can some one help me please ?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
p3(n);
}
public static void p3(int n) {
int myLenth = 2 * n + 1;
int nst = 0;
int space = myLenth - 2;
int secVal = n;
for (int i = 1; i <= myLenth; i++) {
int myN = n;
for (int j = 0; j <= nst; j++) {
System.out.printf(" %d ", myN);
myN--;
}
for (int j = 1; j <= space; j++) {
System.out.printf(" ", space);
}
myN = secVal;
//nst - 1 is to print 1 less column
for (int j = 0; j <= (j == n ? nst - 1 : nst); j++) {
System.out.printf(" %d ", myN);
myN++;
}
System.out.println();
if (i <= myLenth / 2) {
nst += 1;
space -= 2;
if (i != n) {
secVal--;
}
} else {
nst -= 1;
space += 2;
if (i != n + 1) {
secVal++;
}
}
}
}
}