Hollow diamond pattern

import java.util.;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int presentrow,nst=(n/2),cst,nsp=1,csp,i;
for(presentrow=1;presentrow<=n;presentrow++)
{
if(presentrow==1||presentrow==n){
for(i=1;i<=n;i++)
System.out.println();
}
else{
for(cst=1;cst<=nst;cst++){
System.out.print("
");
}
for(csp=1;csp<=nsp;csp++){
System.out.print(" β€œ);
}
for(cst=1;cst<=nst;cst++){
System.out.print(”*");
}
if(presentrow<=(n/2)){
nst–;
nsp=nsp+2;
}
else{
nst++;
nsp=nsp-2;
System.out.println();
}
}
}
}
}

where i went wrong plz tell me

@karthik1989photos,
nst = (n/2 + 1), cst = 1, rows = 1, nsp = -1,total rows = n;

  • The given pattern is seen as first stars then spaces and then stars .So for each row do the work of stars first , then that of spaces and then that of stars. And then change the variables accordingly for next iterations.
  • But this work will not help u to pass the test cases as there will be 1 extra star in the first line and last line. For e.g.,
       * * * * * *  //Extra star
       * *   * *
       *       *
       * *   * *
       * * * * * * // Extra Star
  • To handle this condition all you need to do is run your second look 1 time less only when control is in first and last line.
    if(rows == 1 || rows == n) cst = 2;

https://ide.codingblocks.com/s/262426 you can refer to this as well

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.