How to generalise this help me!

I am able to make logic to solve this kind of pattern problems but please tell me about any hack or something else so that i can generalise these kind of pattern problem …how we generalise such pattern problems …please help sir !!!

In Pattern Question, there is a common method which we teach in course viz, taking nsp, nst, csp, cst and row variables.

Short Info about the variables:-

1. nsp (number of spaces)-> Number of spaces in very First Line of the pattern.
2. nst (number of stars)-> Number of stars in very first line of the pattern.
3. csp (counter of spaces)-> counter of spaces that will print the required number of spaces and will be initialized with 1 and incremented upto nsp.
4. cst (counter of stars)->  counter of spaces that will print the required number of stars and will be initialized with 1 and incremented upto nst.
5. rows -> It will be initialized with 1 and will go upto the total number of rows in the pattern.

Here 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;

Pattern questions are based on observation skills only and there is no specific way to solve pattern problems

Can you please check my code for this problems , how can i convert it into a generalise form (N form)

Please share ur code in IDE, if its convertible then I will convert it.
Otherwise u have to follow the above algo :slight_smile:

hey @piyushkumar999999
here updated ur code and mentioned the changes in comments : https://ide.codingblocks.com/s/353595 :slight_smile:

Hey @piyushkumar999999
If u dont have any other doubt in this then please mark it as resolved :slight_smile:

hi @piyushkumar999999 do you have any further doubts?

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.