Triangular Number Patterns 3
Hello coders!
Finally we reached our last article upon pattern problems but you might be wondering that what's going to be different in this?
In this article, we are going to learn about triangular patterns consisting of 0s and 1s. If you practiced previous articles then you must be familiar with square patterns of 0s and 1s and numeric triangular patterns. It' somewhat like a mix of them. So without waiting any further, let's go 🚀
Problem 1
Input
N=5
Output
1
10
101
1010
10101
Approach
Intuition
Seeing the previous 2 articles on triangular patterns, I have decoded one thing that if I relate the triangular patterns to square ones, things get easier XD
Now, relating it to a previous square pattern given as the second problem in the first article on number patterns :
10101
10101
10101
10101
10101
Please go give it a read, if you haven't done that yet : (
Now, coming back to our current problem, the first approach that comes to my mind is to print the same square pattern but only till certain columns.
For 1st row, print till 1st column
For 2nd row, print till 2nd column
For 3rd row, print till 3rd column
and so on.
Observing the pattern, we see that our range of columns is from 1 to the current row's value meaning the logic for conditions is same as that of the square pattern, but we need to change the range of our columns from 1 to the current row.
But, how do we change our column ranges? I hope you got it right i.e. by changing inner loops! So the range of our inner loop would be from 1 to the current row.
Now coming to the question of what to print? If you have read that square article then you must know but incase you haven't, then simply observe the pattern.
For the 1st, 3rd, and 5th columns, we print 1 and for the 2nd and 4th columns, we print 0. Ring some bells? I hope you got this right: for odd columns, we print 1 and for for even columns we print 0. Hence, inside the inner loop, simply add 1 condition that checks upon the current column no.. If it's not divisible by 2 (odd), print 1 else print 0.
Explanation/Steps
- The pattern consists of total N number of rows. Therefore the outer loop would iterate through rows from i=1 to N.
- Here each row contains exactly i number of columns (where i is the current row number). Hence the inner loop formation to iterate through each columns will be from j=1 to j=i.
- Once you are done with the loop formation to iterate through rows and columns. You need to print the 0’s and 1’s. Notice that here for each odd columns 1 gets printed and for every even columns 0 gets printed. Hence you need to check a simple condition if(j % 2 == 1) before printing 1s or 0s.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
// For every odd column print 1, else print 0
if (j % 2 == 1) {
System.out.print("1");
} else {
System.out.print("0");
}
}
System.out.println();
}
}
}
Problem 2
Input
N = 5
Output
1
00
111
0000
11111
Approach
Intuition
With all the questions that you have practiced till now upon triangular patterns, one thing gets pretty clear that we would be using a nested loop for rows and columns. Also the range of inner loops would range from 1 to current row as row no. = no. of columns in that particular row.
Coming to the pattern, let's relate this problem to the last question. Here we print 0s in our 2nd and 4th row and 1s in 1st, 3rd and 5th rows rather than columns. This means that rather than checking for current column, we would check for current row to see, if it's odd or even. And I hope you know for how to check for odd-even rows and columns (*coughs*, no disappointment please 🥺)
Explanation/Steps
- The pattern consists of N rows. Hence the outer loop will iterate on i through rows from i=1 to N.
- Each row contains exactly i number of columns (where i is the current row number) i.e. first row contains 1 column, 2 row contains 2 column and so on.
Hence the inner loop will iterate on j through columns from j=1 to i. - Once you are done with the loop semantics, you just need to print the number. If you notice the numbers are in special order. For each odd row, 1 gets printed and for each even row, 0 gets printed. Now to implement this we need to check a simple condition if(i % 2 == 1) implying odd row, then print 1 else print 0.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
// Print 1s for every odd row, else print 0
if (i % 2 == 1) {
System.out.print("1");
} else {
System.out.print("0");
}
}
System.out.println();
}
}
}
Problem 3
Input
N = 5
Output
1
01
010
1010
10101
Approach
Intuition
For the loops, we would be using a nested loop for rows and columns. Also the range of inner loops would range from 1 to current row as row no. = no. of columns in that particular row. Once you are done with the loops let's observe the pattern.
Try to imagine this pattern without a triangle, just like a single sequence of no.s. It's simply a sequence of alternate 0s and 1s like: 1010101010101.... Here 1's printed at position no. 1,3,5,7 etc. and 0 at 2,4,6,8 etc. These are simply the odd and even positions of the sequence. But how to keep track of the sequence position.
Remember, in such cases, we use another variable k that tracks the current position. Now we know that for each odd integers 1 gets printed and for each even integers 0 gets printed. Therefore to get such pattern we need to check a simple condition if(k % 2 == 1) before printing 1s or 0s (where k represents the current position/integer).
Explanation/Steps
- The pattern consists of N rows (where N is the total number of rows to be printed). Hence the outer loop formation to iterate through rows will be for(i=1; i<=N; i++).
- Here each row contains exactly i number of columns (where i is the current row number). Loop formation to iterate through columns will be for(j=1; j<=i; j++).
- We'll initiate another variable k = 1 before the loops and keep incrementing it's value after printing any value to keep track of the current position. Inside the inner loop, we'll check if(k % 2 == 1) indicating odd position, then print 1 else print 0.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
// k represents the current integer
int k = 1;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
// Print 1 if the current integer k is odd, else print 0
if (k % 2 == 1) {
System.out.print("1");
} else {
System.out.print("0");
}
k++;
}
System.out.println();
}
}
}
Problem 4
With this, we come to our last pattern of this article and of our pattern series. Come on, let's shatter this problem as well 😤
Input
N = 5
Output
1
11
101
1001
11111
Approach
Hint
Relate this problem to box pattern as done in one of the previous article (Problem 3, Article Name: Number Patterns)
Intuition
For the loops, we would be using a nested loop for rows and columns. Also the range of inner loops would range from 1 to current rows as no. of columns in that particular row = current_row no. Once you are done with the loops, let's observe the pattern.
If you have followed the number pattern articles till now (which I hope you have 😉) then you need to relate this problem to a box pattern. We did this in 3rd problem of our Number Patterns article. The pattern was:
11111
10001
10001
10001
11111
Now if you haven't gone through that then please go once as the intuition and logic for this problem is same. We just need to change the range of our loops to convert this square pattern to triangular one which we already know! You are now ready to move ahead & write the code. If you are confused with the pattern then continue reading : )
Observe the positions where 1 is printed in the given problem. 1 is printed for
- Every column of first and last row.
- Start and end column of each row.
This means that we need to add if else conditions in our inner loop for checking our current rows and columns. We'll use conditions like:
if 'current_row = first_row OR current_column =first_column OR current_row = last_row OR current_column = last_column' then print 1
else print 0.
Explanation/Steps
- The pattern consists of N rows. Outer loop formation to iterate through the rows will be for(i=1; i<=N; i++).
- Each row contains exactly i columns. Hence the loop formation to iterate through individual columns will be for(j=1; j<=i; j++).
- Now comes the logic to print 0 or 1. You can see that 1 only gets printed for first and last column or first and last row otherwise 0 gets printed. Therefore you must check a condition that if(i==1 || i==N || j==1 || j==i) then print 1 otherwise print 0.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
if (i == 1 || i == N || j == 1 || j == i) {
System.out.print("1");
} else {
System.out.print("0");
}
}
System.out.println();
}
}
}
With this, I hope you all grasped the concept of patterns and intense use of loops for solving such problems. We saw and practiced a variety of patterns till now: star patterns - square, rhombus, triangular miscellaneous and number patterns - squares and triangles consisting of 0s & 1s and different range of numbers as well.
You can always change these patterns as much as you want and try out solving new questions out of them.
Keep practicing : )