Skip to main content

Command Palette

Search for a command to run...

How to Solve Pattern Problems in under 2 Minutes

Updated
4 min read
How to Solve Pattern Problems in under 2 Minutes

Most of the time, when we try to solve any pattern problem, we just can't come up with a logic for the problem by looking at a pattern. But after going through this blog, you will be able to solve any pattern problem easily. In this blog, I will share the steps you can take to easily solve any problem.

Pattern 1

Screenshot from 2022-10-07 15-33-15.png

Step 1:

The Outer Loop will iterate the same number of times that the line pattern has. As a given pattern has five lines, the outer loop will iterate five times.

// Outer Loop
for(int y=1; y<=5; y++){
// Inner Loop
      }

Step 2:

Draw the cartesian graph. Take the number of lines on the y-axis and the number of stars on the x-axis.

Frame 1.png

ezgif.com-gif-maker.gif.

Step 3:

Find the equation of the line. This equation will define the conditions for executing the code block.

patternTwo.png $$ y = mx + c$$ $$ y = 1.x + 0$$ $$ x=y $$

If you don't know how to find equation of line, then you can go through this Equation of Line.

Step 4:

Now codeeeehhhhhhh.

// Outer loop
for (int y = 1; y <= 5; y++) {
     // Inner loop
            for (int x = 1; x <=y  ; x++) {
                System.out.print("*");
            }
            System.out.println();
        }

Inside the inner loop, we used the equation that we obtained from Step 3.

This problem was easy. Now we are going to look at problems that have spaces and shows symmetry in the pattern.

Pattern 2

Screenshot from 2022-10-07 17-34-56.png

This pattern is symmetrical on the x-axis at y=5. To solve these type of problem. We create a variable equal to symmetric point.

  int n = 5 ;

Step 1:

// Outer Loop
for(int y=1; y<=2n-1; y++){
// Inner Loop
      }

Step 2:

Frame 4.png

ezgif.com-gif-maker (1).gif

Step 3:

Patter3.png We already declared a variable n at which point the graph is turning, so we'll need to write two equations, one for when y <= n and one for when y > n.

Equation when y <= n:

$$ y = mx + c$$ $$ y = 1.x + 0$$ $$ x=y $$

when y > n:

$$ y = mx + c$$ $$ y = 1.x + 10$$ $$ x=10-y $$ $$ x=2*n-y $$ Note: try to write your equation in terms of 'n'.

Step 4:

We need to declare a variable to run this first equation when y<=n and the second equation when y>n.

int totalStars= y<=n ? y : 2*n-y;

This totalStars variable will have a value of 'y' if y<=n, else it will be equal to 2*n-y. Code for this pattern

 int n=5;
        for (int y = 1; y <= 2*n-1; y++) {
            int totalStars= y<=n ? y : 2*n-y;
            for (int x = 1; x <=totalStars  ; x++) {
                System.out.print("*");
            }
            System.out.println();
        }

Now we going to take pattern having spaces.

Pattern 3

Screenshot from 2022-10-08 11-43-09.png

Step 1:

int n = 5;
// Outer Loop
for(int y=1; y<=5; y++){
// Inner Loop
      }

Step 2:

It has both spaces and stars, so we need to make a graph of both of them.

Frame 2.png Now make a graph for Spaces.

ezgif.com-gif-maker (2).gif

Graph for the Stars will be same as the graph of stars in pattern 1.

Step 3:

For the stars equation: $$ y = mx + c$$ $$ y = 1.x + 0$$ $$ x=y $$ For the spaces equation: SpaceGraph.png $$ y = mx + c$$ $$ y = -1.x + 5$$ $$ x=5-y $$ $$ x=n-y $$

Step 4:

Coddeeehhh

 int n = 5;
        for (int y = 1; y <= n; y++) {
            for (int space = 1; space <= n-y; space++) {
                System.out.print(" ");
            }
            for (int x = 1; x <=y  ; x++) {
                System.out.print("*");
            }
            System.out.println();
        }

Takeaway

To solve any pattern problem you can follow these simple steps:

  • The Outer Loop will iterate the same number of times that the line pattern has.

  • Draw the cartesian graph.

  • Find the equation of the line. This equation will define the conditions for executing the code block.

  • Codehhhhh.

If you want more solutions by this mehtod, you can visit this Pattern Problem. There you can find more solutions to the problem.