Pattern 7

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 create a pattern of * and separated by tab as shown in output format.
Input Format
A number n
Output Format
pat71
Question Video
Constraints
1 <= n <= 100
Sample Input
5
Sample Output
*	
*
*
*
*


  • Editorial

    "A design adds value faster than it adds cost."

    Question:

    1. You are given a number n.

    2. You have to create a pattern of * and separated it by tab as shown in the output format.

    Input format:

    A number n

    Output format:


    Constraints:

    1 <= n <= 100

    Solution Approach:

    We have to print a diagonal in this problem.

    Let us analyze the required output format.


    We realize that the star has to be printed on a row has to be printed on the same column number.

    We recognize this design structure and try to use it for our loop formation. This information tells us that when the value of our outer loop iterator ('i' for instance) and inner loop iterator( 'j' for instance) are the same, we will be at the desired location on the console output to print star. This will give us our desired diagonal.

    But first, let us understand the programming procedure to reach this step.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            for(int i =1;i<=n;i++)      //decides the number of rows for output printing
                                            {
                                                for(int j =1;j<=n;j++)  //prints the stars in the row
                                                {
                                                    System.out.print("*	");
                                                }
                                                System.out.println();   //changes the row on output console
                                            }
                                        }
                                    }

    java false

    This program will print us a square of 'n' stars.


    But we know we have to print the star only when value of i = value of j; and leave a space (tab) rest of the times.

    Making adjustments in the code to accommodate the same:

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            for(int i =1;i<=n;i++)      //decides the number of rows for output printing
                                            {
                                                for(int j =1;j<=n;j++)  //prints the stars in the row
                                                {
                                                    if(i == j)                  //when row number == column number
                                                        System.out.print("*	");
                                                    else                        //when row number != column number
                                                        System.out.print("	");    
                                                }
                                                System.out.println();   //changes the row on output console
                                            }
                                        }
                                    }

    java false

    Output(for input value n =5):


    We can optimize this code a little from time complexity perspective, by breaking out of the inner loop as soon as we print the star, because we do not really need to print all the whitespaces in the output.

    Optimized Code:

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            for(int i =1;i<=n;i++)      //decides the number of rows for output printing
                                            {
                                                for(int j =1;j<=n;j++)  //prints the stars in the row
                                                {
                                                    if(i == j)                  //when row number == column number
                                                        {
                                                            System.out.print("*	");
                                                            break;              // break out of the loop as star is printed
                                                        }
                                                    else                        //when row number != column number
                                                        System.out.print("	");    
                                                }
                                                System.out.println();   //changes the row on output console
                                            }
                                        }
                                    }

    java false

    Output:



    If you notice the time taken in previous output and this output, you can clearly notice, that this code is much faster than the previous one.

    Although both codes are absolutely correct; A good programmer should always try to optimize the time complexity or space complexity of program after obviously, taking care that the program yields correct output.

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name