Given an integer N which is the number of rows, the task is to draw the number pattern in the shape of a double headed arrow.
Prerequisite: The pattern is a grow and shrink type pattern and hence basic knowledge to execute loops is required to understand the topic and the code in any language. The geometric shape can be visualized as-
Examples:
Input: R = 7 Output: 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 Input: R = 9 Output: 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1
Approach:
- In the given example, N=7 and the number of ROWS is 7.
- VERTICALLY, the pattern GROWS till ROW=N/2 and SHRINKS afterwards.
- ROW 1 has 4 ” “(SPACE) characters and then a value.
- The number of SPACE characters decreases whereas NUMERALS increase in count in each successive row.
- Also, note that the 1st value of the number placed in each row is the same as the number of the row.
- Also HORIZONTALLY the pattern has NUMERALS, then SPACES and NUMERALS afterwards.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to print the required pattern void drawPattern( int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { cout << " " << " " ; csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { cout << val1 << " " ; val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { cout << " " << " " ; csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { cout << val2 << " " ; val2 = val2 + 1; cst2 = cst2 + 1; } } cout << endl; // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code int main() { // Number of rows int N = 7; drawPattern(N); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to print the required pattern static void drawPattern( int N) { int n = N; int row = 1 ; // 'nst' is the number of values int nst = 1 ; // 'nsp' is the number of spaces int nsp1 = n - 1 ; int nsp2 = - 1 ; int val1 = row; int val2 = 1 ; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1 ; while (csp1 <= nsp1) { System.out.print( " " ); csp1 = csp1 + 1 ; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1 ; while (cst1 <= nst) { System.out.print(val1 + " " ); val1 = val1 - 1 ; cst1 = cst1 + 1 ; } // Again spaces have to be printed int csp2 = 1 ; while (csp2 <= nsp2) { System.out.print( " " ); csp2 = csp2 + 1 ; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1 ; while (cst2 <= nst) { System.out.print(val2 + " " ); val2 = val2 + 1 ; cst2 = cst2 + 1 ; } } System.out.println(); // Move to the next row if (row <= n / 2 ) { nst = nst + 1 ; nsp1 = nsp1 - 2 ; nsp2 = nsp2 + 2 ; val1 = row + 1 ; val2 = 1 ; } else { nst = nst - 1 ; nsp1 = nsp1 + 2 ; nsp2 = nsp2 - 2 ; val1 = n - row; val2 = 1 ; } row = row + 1 ; } } // Driver code public static void main(String args[]) { // Number of rows int N = 7 ; drawPattern(N); } } |
Python3
# Python3 implementation of the approach # Function to print the required pattern def drawPattern(N) : n = N; row = 1 ; # 'nst' is the number of values nst = 1 ; # 'nsp' is the number of spaces nsp1 = n - 1 ; nsp2 = - 1 ; val1 = row; val2 = 1 ; while (row < = n) : # Here spaces are printed # 'csp' is the count of spaces csp1 = 1 ; while (csp1 < = nsp1) : print ( " " ,end = " " ); csp1 = csp1 + 1 ; # Now, values are printed # 'cst' is the count of stars cst1 = 1 ; while (cst1 < = nst) : print (val1,end = " " ); val1 = val1 - 1 ; cst1 = cst1 + 1 ; # Again spaces have to be printed csp2 = 1 ; while (csp2 < = nsp2) : print ( " " ,end = " " ); csp2 = csp2 + 1 ; # Again values have to be printed if (row ! = 1 and row ! = n) : cst2 = 1 ; while (cst2 < = nst) : print (val2,end = " " ); val2 = val2 + 1 ; cst2 = cst2 + 1 ; print () # Move to the next row if (row < = n / / 2 ) : nst = nst + 1 ; nsp1 = nsp1 - 2 ; nsp2 = nsp2 + 2 ; val1 = row + 1 ; val2 = 1 ; else : nst = nst - 1 ; nsp1 = nsp1 + 2 ; nsp2 = nsp2 - 2 ; val1 = n - row; val2 = 1 ; row = row + 1 ; # Driver code if __name__ = = "__main__" : # Number of rows N = 7 ; drawPattern(N); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function to print the required pattern static void drawPattern( int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { Console.Write( " " ); csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { Console.Write(val1 + " " ); val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { Console.Write( " " ); csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { Console.Write(val2 + " " ); val2 = val2 + 1; cst2 = cst2 + 1; } } Console.WriteLine(); // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code public static void Main() { // Number of rows int N = 7; drawPattern(N); } } // This code is contributed by AnkitRai01 |
Javascript
<script> // JavaScript implementation of the approach // Function to print the required pattern function drawPattern(N) { var n = N; var row = 1; // 'nst' is the number of values var nst = 1; // 'nsp' is the number of spaces var nsp1 = n - 1; var nsp2 = -1; var val1 = row; var val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces var csp1 = 1; while (csp1 <= nsp1) { document.write( " " + " " ); csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars var cst1 = 1; while (cst1 <= nst) { document.write(val1 + " " ); val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed var csp2 = 1; while (csp2 <= nsp2) { document.write( " " + " " ); csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { var cst2 = 1; while (cst2 <= nst) { document.write(val2 + " " ); val2 = val2 + 1; cst2 = cst2 + 1; } } document.write( "<br>" ); // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code // Number of rows var N = 7; drawPattern(N); </script> |
1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1
Time Complexity: O(N2)
Space Complexity: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!