Table of contents
Introduction
Pattern questions are a type of programming problem that require you to print a specific pattern using a combination of symbols, characters, or numbers. These questions are often used in programming competitions, coding interviews, and academic assignments to test a candidate's problem-solving skills and understanding of loops and conditional statements.
Pattern questions can vary in complexity, and they can be categorized into different types based on the shape of the pattern. For example, some common pattern shapes include:
Rectangular patterns
Triangular patterns
Diamond patterns
Arrow patterns
Pyramid patterns
To solve a pattern question, you need to break it down into smaller parts and use loops and conditional statements to print the required pattern. The most common types of loops used to solve pattern questions are the for loop and the while loop.
Approach
A pattern is made up of rows and columns. The pattern's lines, which correspond to its rows, would iterate the same number of times as the outer loop. Following that, we would determine the number of columns and the type of elements used in the pattern. The inner loop would iterate by the number of columns in the pattern. The only thing we need to do is establish a relationship between the row index and column index so that the element prints according to the pattern.
The outer loop
As was explained earlier, the outer loop would repeat as many times as there are lines in the pattern. Here are a few illustrations to assist illustrate this.
*
**
***
****
*****
The outer loop iterates five times for the aforementioned pattern.
*
**
***
****
*****
*****
****
***
**
*
In this scenario, we can see that the pattern repeats after five rows. As a result, the number of outer loop iterations is '2n' where n=5.
The inner loop
Let's look at an example to better grasp the relationship between the outer loop index and the inner loop index.
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
The outer loop would iterate 2n times, where n=5. And the inner loop would iterate n times.
We can see that there are spaces in each row, and they also follow a pattern. Until n, the number of spaces is equal to the index number; then we can simply reverse the pattern by printing index-(n%index) spaces.
Furthermore, the number of asterisks is equal to n-index until n, after which we can reverse the pattern by printing (index%n) asterisks.
The code for the above pattern by the provided technique is as follows:
int n=5, row=0, column=0;
for(row=0; row<2*n; row++){
int noofasterisks= row>=n? (row%n)+1 : n-row;
int noofspaces= row>=n? n-(row%n)-1: row;
for(column=0; column<noofspaces; column++){
System.out.print(" ");
}
for(column=0; column<noofasterisks; column++){
System.out.print("* ");
}
System.out.println();
}
Conclusion
As implied by the name, there is always some sort of pattern in the pattern questions; we need to determine how that pattern relates to the index value. Solving pattern questions can improve your problem-solving skills and make you more proficient in writing clean and efficient code. By practicing different pattern questions and becoming familiar with different techniques and strategies, you can increase your confidence and ability to tackle more complex programming challenges.
In addition to improving your programming skills, solving pattern questions can also help you in coding interviews, programming competitions, and academic assignments. Therefore, it's important to practice pattern questions and be familiar with different types of patterns and their solutions.
Examples
*****
*****
*****
*****
*****
for (int row = 1; row <= n; row++) {
// for every row, run the col
for (int col = 1; col <= n; col++) {
System.out.print("* ");
}
// when one row is printed, we need to add a newline
System.out.println();
}
*
**
***
****
*****
for (int row = 1; row <= n; row++) {
// for every row, run the col
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
// when one row is printed, we need to add a newline
System.out.println();
}
*****
****
***
**
*
for (int row = 1; row <= n; row++) {
// for every row, run the col
for (int col = 1; col <= n-row+1; col++) {
System.out.print("* ");
}
// when one row is printed, we need to add a newline
System.out.println();
}
1
12
123
1234
12345
for (int row = 1; row <= n; row++) {
// for every row, run the col
for (int col = 1; col <= row; col++) {
System.out.print(col + " ");
}
// when one row is printed, we need to add a newline
System.out.println();
}
*
**
***
****
*****
****
***
**
*
for (int row = 0; row < 2 * n; row++) {
int totalColsInRow = row > n ? 2 * n - row: row;
for (int col = 0; col < totalColsInRow; col++) {
System.out.print("* ");
}
System.out.println();
}
1
212
32123
4321234
32123
212
1
for (int row = 1; row <= 2 * n; row++) {
int c = row > n ? 2 * n - row: row;
for (int space = 0; space < n-c; space++) {
System.out.print(" ");
}
for (int col = c; col >= 1; col--) {
System.out.print(col + " ");
}
for (int col = 2; col <= c; col++) {
System.out.print(col + " ");
}
System.out.println();
}
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
for (int row = 0; row < 2 * n; row++) {
int totalColsInRow = row > n ? 2 * n - row: row;
int noOfSpaces = n - totalColsInRow;
for (int s = 0; s < noOfSpaces; s++) {
System.out.print(" ");
}
for (int col = 0; col < totalColsInRow; col++) {
System.out.print("* ");
}
System.out.println();
}
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
for (int row = 1; row <= n; row++) {
for (int space = 0; space < n-row; space++) {
System.out.print(" ");
}
for (int col = row; col >= 1; col--) {
System.out.print(col + " ");
}
for (int col = 2; col <= row; col++) {
System.out.print(col + " ");
}
System.out.println();
}
4 4 4 4 4 4 4 4 4
4 3 3 3 3 3 3 3 4
4 3 2 2 2 2 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 1 0 1 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 2 2 2 2 3 4
4 3 3 3 3 3 3 3 4
4 4 4 4 4 4 4 4 4
int originalN = n;
n = 2 * n;
for (int row = 0; row <= n; row++) {
for (int col = 0; col <= n; col++) {
int atEveryIndex = originalN - Math.min(Math.min(row, col), Math.min(n - row, n - col));
System.out.print(atEveryIndex + " ");
}
System.out.println();
}