Program to print double headed arrow pattern
Last Updated :
11 Jun, 2021
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++
#include <iostream>
using namespace std;
void drawPattern( int N)
{
int n = N;
int row = 1;
int nst = 1;
int nsp1 = n - 1;
int nsp2 = -1;
int val1 = row;
int val2 = 1;
while (row <= n) {
int csp1 = 1;
while (csp1 <= nsp1) {
cout << " "
<< " " ;
csp1 = csp1 + 1;
}
int cst1 = 1;
while (cst1 <= nst) {
cout << val1 << " " ;
val1 = val1 - 1;
cst1 = cst1 + 1;
}
int csp2 = 1;
while (csp2 <= nsp2) {
cout << " "
<< " " ;
csp2 = csp2 + 1;
}
if (row != 1 && row != n) {
int cst2 = 1;
while (cst2 <= nst) {
cout << val2 << " " ;
val2 = val2 + 1;
cst2 = cst2 + 1;
}
}
cout << endl;
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;
}
}
int main()
{
int N = 7;
drawPattern(N);
return 0;
}
|
Java
class GFG {
static void drawPattern( int N)
{
int n = N;
int row = 1 ;
int nst = 1 ;
int nsp1 = n - 1 ;
int nsp2 = - 1 ;
int val1 = row;
int val2 = 1 ;
while (row <= n) {
int csp1 = 1 ;
while (csp1 <= nsp1) {
System.out.print( " " );
csp1 = csp1 + 1 ;
}
int cst1 = 1 ;
while (cst1 <= nst) {
System.out.print(val1 + " " );
val1 = val1 - 1 ;
cst1 = cst1 + 1 ;
}
int csp2 = 1 ;
while (csp2 <= nsp2) {
System.out.print( " " );
csp2 = csp2 + 1 ;
}
if (row != 1 && row != n) {
int cst2 = 1 ;
while (cst2 <= nst) {
System.out.print(val2 + " " );
val2 = val2 + 1 ;
cst2 = cst2 + 1 ;
}
}
System.out.println();
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 ;
}
}
public static void main(String args[])
{
int N = 7 ;
drawPattern(N);
}
}
|
Python3
def drawPattern(N) :
n = N;
row = 1 ;
nst = 1 ;
nsp1 = n - 1 ;
nsp2 = - 1 ;
val1 = row;
val2 = 1 ;
while (row < = n) :
csp1 = 1 ;
while (csp1 < = nsp1) :
print ( " " ,end = " " );
csp1 = csp1 + 1 ;
cst1 = 1 ;
while (cst1 < = nst) :
print (val1,end = " " );
val1 = val1 - 1 ;
cst1 = cst1 + 1 ;
csp2 = 1 ;
while (csp2 < = nsp2) :
print ( " " ,end = " " );
csp2 = csp2 + 1 ;
if (row ! = 1 and row ! = n) :
cst2 = 1 ;
while (cst2 < = nst) :
print (val2,end = " " );
val2 = val2 + 1 ;
cst2 = cst2 + 1 ;
print ()
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 ;
if __name__ = = "__main__" :
N = 7 ;
drawPattern(N);
|
C#
using System;
class GFG
{
static void drawPattern( int N)
{
int n = N;
int row = 1;
int nst = 1;
int nsp1 = n - 1;
int nsp2 = -1;
int val1 = row;
int val2 = 1;
while (row <= n)
{
int csp1 = 1;
while (csp1 <= nsp1)
{
Console.Write( " " );
csp1 = csp1 + 1;
}
int cst1 = 1;
while (cst1 <= nst)
{
Console.Write(val1 + " " );
val1 = val1 - 1;
cst1 = cst1 + 1;
}
int csp2 = 1;
while (csp2 <= nsp2)
{
Console.Write( " " );
csp2 = csp2 + 1;
}
if (row != 1 && row != n)
{
int cst2 = 1;
while (cst2 <= nst)
{
Console.Write(val2 + " " );
val2 = val2 + 1;
cst2 = cst2 + 1;
}
}
Console.WriteLine();
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;
}
}
public static void Main()
{
int N = 7;
drawPattern(N);
}
}
|
Javascript
<script>
function drawPattern(N) {
var n = N;
var row = 1;
var nst = 1;
var nsp1 = n - 1;
var nsp2 = -1;
var val1 = row;
var val2 = 1;
while (row <= n) {
var csp1 = 1;
while (csp1 <= nsp1) {
document.write( " " + " " );
csp1 = csp1 + 1;
}
var cst1 = 1;
while (cst1 <= nst) {
document.write(val1 + " " );
val1 = val1 - 1;
cst1 = cst1 + 1;
}
var csp2 = 1;
while (csp2 <= nsp2) {
document.write( " " + " " );
csp2 = csp2 + 1;
}
if (row != 1 && row != n) {
var cst2 = 1;
while (cst2 <= nst) {
document.write(val2 + " " );
val2 = val2 + 1;
cst2 = cst2 + 1;
}
}
document.write( "<br>" );
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;
}
}
var N = 7;
drawPattern(N);
</script>
|
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
Time Complexity: O(N2)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...