Java Program to print number pattern
The following article explores about each row forms a symmetric palindrome. The middle column always contains the digit 1, flanked by columns on either side that contain constant digits greater than 1. As the pattern expands, the constant digits increase sequentially outward from the center. The input determines the size of the pattern, dictating the number of rows and the maximum constant digit value. The above pattern showcases both symmetry and a gradual increase in digit values, creating an aesthetically pleasing arrangement. Let's delve into the intricacies of generating such a pattern for any given input size.
Example:
Input: n=4
Output:
1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 3 2 1 2 3 2 1 2 1
Using Naive Approach
Algorithm
Step 1: Input for pattern size, denoted as 'n'.
Step 2: Iterate over each row of the upper half of the pattern from 1 to 'n'.
Step 3: Print spaces before numbers, decreasing from 'n' to 'i'.
Step 4: Print left-side numbers in decreasing order from 'i' to 2.
Step 5: Print the constant digit '1' for the middle column.
Step 6: Print right side numbers in increasing order from 2 to 'i'.
Step 7: Iterate over each row of the lower half from 'n-1' to 1.
Step 8: Print spaces before numbers, increasing from 'n-1' to 'i'.
Step 9: Repeat steps 2 and 3 until all rows are printed.
Here is an implementation of the printing number pattern using a naive approach:
FileName:NumberPattern.java
public class NumberPattern { public static void main(String[] args) { int n = 3; // Printing upper half for (int i = 1; i <= n; i++) { // Printing spaces before numbers for (int j = i; j < n; j++) System.out.print(" "); // Printing left side numbers for (int j = i; j >= 2; j--) System.out.print(j + " "); // Printing middle column with 1 System.out.print("1 "); // Printing right side numbers for (int j = 2; j <= i; j++) System.out.print(j + " "); System.out.println(); } // Printing lower half for (int i = n - 1; i >= 1; i--) { // Printing spaces before numbers for (int j = i; j < n; j++) System.out.print(" "); // Printing left side numbers for (int j = i; j >= 2; j--) System.out.print(j + " "); // Printing middle column with 1 System.out.print("1 "); // Printing right side numbers for (int j = 2; j <= i; j++) System.out.print(j + " "); System.out.println(); } } }
Output:
1 2 1 2 3 21 2 3 2 1 2 1
Complexity analysis: The time complexity of the above approach is O(n^2), where n is the input size. It is because there are nested loops iterating over the rows and columns of the pattern, each of which runs for n iterations in the worst case, and the space complexity of the code is O(1) since the amount of memory used remains constant regardless of the input size.
Using Optimized Approach
Algorithm
Step 1: Initialize variable 'n' to denote pattern size.
Step 2: Iterate over each row of the upper half of the pattern from 1 to 'n'.
Step 3: Print spaces before numbers, decrementing from 'n' to 'i'.
Step 4: Print left-side numbers in decreasing order from 'i' to 2.
Step 5: Print the constant digit '1' for the middle column.
Step 6: Print right side numbers in increasing order from 2 to 'i'.
Step 7: Iterate over each row of the lower half from 'n-1' to 1.
Step 8: Print spaces before numbers, incrementing from 'n-1' to 'i'.
Step 9: Repeat steps 2 and 3 until all rows are printed.
Here is an implementation of the printing number pattern using an optimized approach:
FileName:NumberPattern1.java
public class NumberPattern1 { public static void main(String[] args) { int n = 3; // Printing upper half for (int i = 1; i <= n; i++) { // Printing spaces before numbers for (int j = 1; j <= n - i; j++) System.out.print(" "); // Printing left side numbers for (int j = i; j >= 2; j--) System.out.print(j + " "); // Printing middle column with 1 System.out.print("1 "); // Printing right side numbers for (int j = 2; j <= i; j++) System.out.print(j + " "); System.out.println(); } // Printing lower half for (int i = n - 1; i >= 1; i--) { // Printing spaces before numbers for (int j = 1; j <= n - i; j++) System.out.print(" "); // Printing left side numbers for (int j = i; j >= 2; j--) System.out.print(j + " "); // Printing middle column with 1 System.out.print("1 "); // Printing right side numbers for (int j = 2; j <= i; j++) System.out.print(j + " "); System.out.println(); } } }
Output:
1 2 1 2 3 2 1 2 3 2 1 2 1
Complexity analysis: The time complexity of the above approach is O(n^2), where n is the input size. It is because there are nested loops iterating over the rows and columns of the pattern, each of which runs for n iterations in the worst case, and the space complexity of the code is O(1) since the amount of memory used remains constant regardless of the input size.