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

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.

.
Step 3:
Find the equation of the line. This equation will define the conditions for executing the code block.
$$ 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

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:


Step 3:
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

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.
Now make a graph for Spaces.

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:
$$ 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.


