Pattern magic logic

i cant able to understand the algorithm or solution code of the question pattern magic

Approach
break the pattern in two halfs -> upper half and lower half
now in each half you have to print

  1. n-i stars
  2. 2*i-1 spaces
  3. n-i stars (but in first row there is one less star so consider that also)

copy the above code and reverse the loop of i for Lower portion

Code

the solution of this question is provided in java but i want in c++ language

No i have provided the c++ Code

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.

https://ide.codingblocks.com/s/633807 i am not getting correct output…plz update it where i m wrong

#include

using namespace std;

int main() {

int n;

cin>>n;

for(int i=0;i<n;i++){

    for(int j=i;j<n;j++){

        cout<<'*';

    }

    for(int k=1;k<=(2*i)-1;k++){

        cout<<' ';

    }

    if(i==0){

        for(int j=i;j<n-1;j++){ 

         cout<<'*';

        }

    }

    else{

        for(int j=i;j<n;j++){   

         cout<<'*';

        }

    }

    cout<<endl;

}

for(int i=n-2;i>=0;i--){

    for(int j=i;j<n;j++){

        cout<<'*';

    }

    for(int k=1;k<=(2*i)-1;k++){

        cout<<' ';

    }

    if(i==0){

        for(int j=i;j<n-1;j++){ 

         cout<<'*';

        }

    }

    else{

        for(int j=i;j<n;j++){   

         cout<<'*';

        }

    }

    cout<<endl;

}

return 0;

}