my code is working as needed but at submission time only case 1 is correct and other all are wrong
Code working but problem in submission time
@deepgarg46 There are two changes that you have to make.
i) Where you have given 1 space there should be 2 spaces there.
ii) and whenever you print and you have to print it like System.out.print(ans+" ");
//Spacing has to be exactly like the sample testcase.
Corrected code is below :
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int nst = 1;
int nsp = n - 1;
int nsp2 = -1;
int row = 1;
while (row <= n) {
int csp = 1;
while (csp <= nsp) {
System.out.print(" ");
csp++;
}
int ans = 0;
int cst = 1;
while (cst <= nst) {
ans=cst;
cst++;
}
cst = 1;
while(ans>=cst) {
System.out.print(ans+" ");
ans--;
}
csp = 1;
while (csp <= nsp2) {
System.out.print(" ");
csp++;
}
cst = 1;
while (cst <= nst) {
if (row == 1 || row == n) {
cst++;
} else {
System.out.print(cst+" ");
cst++;
}
}
System.out.println();
if (row <= n / 2) {
nst++;
nsp -= 2;
nsp2 += 2;
} else {
nst--;
nsp += 2;
nsp2 -= 2;
}
row++;
}
}
}
1 Like
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.