Pattern 16

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

1. You are given a number n.
2. You've to write code to print the pattern given in output format below




Input Format
A number n
Output Format
pat161
Question Video
Constraints
1 <= n <= 10
Sample Input
7
Sample Output
1												1	
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1


  • Editorial

    "Strategy is a pattern in a stream of decisions."

    Question:

    1. You are given a number n.

    2. You have to write code to print the pattern given in the output format below.

    Input format:

    A number n

    Output format:


    Constraints:

    1 <= n <= 10

    Solution Approach:

    For the given input value 'n'; we observe there are 'n' rows and 2n-1 columns in the required output format.

    Now if we look at the number of spaces in the first row; there are 2n-3 spaces.

    Before we learn how to display the content in the desired pattern, we first focus on how to draw the shape of console output using stars.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int sp = 2*n-3;
                                            int st = 1;
                                            for(int i =1;i<=n;i++)
                                            {
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print("*	");
                                                }
                                    
                                                for(int j =1;j<=sp;j++)
                                                {
                                                    System.out.print("	");
                                                }
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print("*	");
                                                }
                                                st++;
                                                sp -= 2;
                                                System.out.println();
                                            }
                                        }
                                    }

    java false

    This program yields us the following output ( for input value 'n' = 5):

    As we can see, it prints an extra star in the last row.


    Let us analyze how this code is working in the RAM, iteration-by-iteration:

    The -1 in the space variable is meaningless and the same as 0; printing no spaces.

    The loop ends as soon as 'i' becomes greater than 'n'.

    For handling the extra star output in the last row as shown in the above output, we put a conditional check to see if a row is the last row just before the second phase of printing stars and if it checks out to be true, we reduce the number of stars by 1, to accommodate the adjustment of the extra star.

    The changes in the code will be as follows:

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int sp = 2*n-3;
                                            int st = 1;
                                            for(int i =1;i<=n;i++)
                                            {
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print("*	");
                                                }
                                    
                                                for(int j =1;j<=sp;j++)
                                                {
                                                    System.out.print("	");
                                                }
                                                if( i == n)	//Last Row check
                                                {
                                                    st--;
                                                }
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print("*	");
                                                }
                                                st++;
                                                sp -= 2;
                                                System.out.println();
                                            }
                                        }
                                    }

    java false
    It yields the output in the required shape:


    As we have sorted out the matter of shape, now we can bring the contents of the shape to our focus.

    Much like the previous problems that we have tackled, we first emphasized on printing the required shape with stars, then placing constant value 1 in it, and then working with variables in place of constants to introduce sequences in the pattern.

    So first, we replace the star by 1 to print a pattern of 1 in the desired shape.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int sp = 2*n-3;
                                            int st = 1;
                                            for(int i =1;i<=n;i++)
                                            {
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print("1	");
                                                }
                                    
                                                for(int j =1;j<=sp;j++)
                                                {
                                                    System.out.print("	");
                                                }
                                                if( i == n)     //Last Row check
                                                {
                                                    st--;
                                                }
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print("1	");
                                                }
                                                st++;
                                                sp -= 2;
                                                System.out.println();
                                            }
                                        }
                                    }

    java false

     output:


    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int sp = 2*n-3;
                                            int st = 1;
                                            for(int i =1;i<=n;i++)
                                            {
                                                int val =1;     //since each row begins with 1
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print(val + "	");
                                                    val++;
                                                }
                                    
                                                for(int j =1;j<=sp;j++)
                                                {
                                                    System.out.print("	");
                                                }
                                                if( i == n)     //Last Row check
                                                {
                                                    st--;       //removing extra star
                                                }
                                                for(int j=1;j<=st;j++)
                                                {
                                                    val--;      //reducing value first then printing
                                                    System.out.print(val + "	");
                                                    
                                                }
                                                st++;
                                                sp -= 2;
                                                System.out.println();
                                            }
                                        }
                                    }

    java false

    But this code comes short on output expectation in the last row again; giving the following output:


    This is because the value is not reduced again, much like the star printing anomaly.

    Since the anomaly is alike, so is the solution; we reduce the value of Val by 1 if the row checks out to be the last row.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int sp = 2*n-3;
                                            int st = 1;
                                            for(int i =1;i<=n;i++)
                                            {
                                                int val =1;     //since each row begins with 1
                                                for(int j=1;j<=st;j++)
                                                {
                                                    System.out.print(val + "	");
                                                    val++;
                                                }
                                    
                                                for(int j =1;j<=sp;j++)
                                                {
                                                    System.out.print("	");
                                                }
                                                if( i == n)     //Last Row check
                                                {
                                                    st--;       //removing extra star
                                                    val--;      //reducing val by 1
                                                }
                                                for(int j=1;j<=st;j++)
                                                {
                                                    val--;      //reducing value first then printing
                                                    System.out.print(val + "	");
                                                    
                                                }
                                                st++;
                                                sp -= 2;
                                                System.out.println();
                                            }
                                        }
                                    }

    java false

    The above makes up our final solution code as it provides the required output for any given valid input.

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name