Discuss, I am running a code which is giving me perfect output , still I am getting all the test cases wrong


Is there any way to share my code with you ?

import java.util.*; public class Main { public static void main(String args[]) { Scanner sc= new Scanner(System.in); int N = sc.nextInt(); int n=(N/2+1); for(int i=1;i<=n;i++) { for(int j=n;j>=i;j–) { System.out.print(" “+” “); System.out.print(” “+” “); }int a=i; for(int k=1;k<=i;k++) { System.out.print(a+” “); a=a-1; } System.out.print(” “); for(int m=3;m<=i;m++) { System.out.print(” “+” “); System.out.print(” “+” “); }int b=1; for(int l=1;l<=i;l++) { if(i==1) { }else { System.out.print(” “+b); b++; }} System.out.println(); } for(int i=2;i<=n;i++) { for(int j=1;j<=i;j++) { System.out.print(” “+” “); System.out.print(” “+” “); } int b=n-i+1; for(int k=n;k>=i;k–) { System.out.print(b+” “); b–; } System.out.print(” “); for(int l=n-2;l>=i;l–) { if(i==(n-1)) { }else { System.out.print(” “+” “); System.out.print(” “+” “); } }int a=1; for(int m=n;m>=i;m–) { if(i==n) { }else { System.out.print(” "+a); a++; } }System.out.println(); } } }

import java.util.*; public class Main { public static void main(String args[]) { Scanner sc= new Scanner(System.in); int N = sc.nextInt(); int n=(N/2+1); for(int i=1;i<=n;i++) { for(int j=n;j>=i;j–) { System.out.print(" “+” “); System.out.print(” “+” “); }int a=i; for(int k=1;k<=i;k++) { System.out.print(a+” “); a=a-1; } System.out.print(” “); for(int m=3;m<=i;m++) { System.out.print(” “+” “); System.out.print(” “+” “); }int b=1; for(int l=1;l<=i;l++) { if(i==1) { }else { System.out.print(” “+b); b++; }} System.out.println(); } for(int i=2;i<=n;i++) { for(int j=1;j<=i;j++) { System.out.print(” “+” “); System.out.print(” “+” “); } int b=n-i+1; for(int k=n;k>=i;k–) { System.out.print(b+” “); b–; } System.out.print(” “); for(int l=n-2;l>=i;l–) { if(i==(n-1)) { }else { System.out.print(” “+” “); System.out.print(” “+” “); } }int a=1; for(int m=n;m>=i;m–) { if(i==n) { }else { System.out.print(” "+a); a++; } }System.out.println(); } } } This is the working code for which I am getting all the test cases failed. Can you please resolve this for me.

you are printing extra spaces
for input 3

the answer should be

    1  
2 1   1 2
    1  

but you are giving

        1  
    2 1   1 2
        1  

remove that extra space .

also to share your code

  • go to ide.codingblocks.com
  • paste your code there.
  • press ctrl s and save
  • hare the updated url from the search bar here