Here my test case 2 is failed why?

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 nst = n;
	int row = 1;
	while(row<=n){
		int cst=1;
		while(cst<=nst){
			
			if(row==n-1||row==2) {
				if(cst==n/2+1) {
					
					System.out.print("    ");
					
				}
				else {
					System.out.print("*   ");
				}
			}
			
			else if(row==n/2+1) {
				if(cst==1||cst==nst) {
					System.out.print("*   ");
				}
				else {
					System.out.print("    ");
				}
			}
			
			else {
				System.out.print("*   ");
			}
			
			
			cst++;
			
			}
		System.out.println();
		row++;
	}
}

}

Hi, you have designed your code specifically for n=5(sample test case).
you need to generalize your code instead of hardcoding it for n=5.
you can try to solve it in two phases.

  1. generate first half part(till n/2 rows)
    you can see that there is " " growing from the middle of column. your condition can be something like if cst is between n/2-rowno to n/2+rowno (you need to fit accurate maths in it), then print " ", otherwise “*”.
  2. then try to generate other half part(row n/2 to n):
    in this case no of " " are contracting. its more like rowno-n/2 to rowno+n/2 condition.

Think about it and try to implement it yourself.
Thanks.

please resolve it and rate it if satisfied.
Thanks

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.