Printing Pattern using Loops - Hacker rank Solution


Printing Pattern using Loops - Hacker rank Solution



Problem


In this problem, you need to print the pattern of the following form containing the numbers from 1 to n.

                            4 4 4 4 4 4 4  
                            4 3 3 3 3 3 4   
                            4 3 2 2 2 3 4   
                            4 3 2 1 2 3 4   
                            4 3 2 2 2 3 4   
                            4 3 3 3 3 3 4   
                            4 4 4 4 4 4 4 
 

Input Format

The input will contain a single integer n.

Constraints

1<=n<=1000

Output Format

Print the pattern mentioned in the problem statement.



Example 0 :

Sample Input 0
2

Sample Output 0
2 2 2
2 1 2
2 2 2

Example 1 : 

Sample Input 1
5

Sample Output 1
5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5

Example 2 :

Sample Input 2
7

Sample Output 2
7 7 7 7 7 7 7 7 7 7 7 7 7 
7 6 6 6 6 6 6 6 6 6 6 6 7 
7 6 5 5 5 5 5 5 5 5 5 6 7 
7 6 5 4 4 4 4 4 4 4 5 6 7 
7 6 5 4 3 3 3 3 3 4 5 6 7 
7 6 5 4 3 2 2 2 3 4 5 6 7 
7 6 5 4 3 2 1 2 3 4 5 6 7 
7 6 5 4 3 2 2 2 3 4 5 6 7 
7 6 5 4 3 3 3 3 3 4 5 6 7 
7 6 5 4 4 4 4 4 4 4 5 6 7 
7 6 5 5 5 5 5 5 5 5 5 6 7 
7 6 6 6 6 6 6 6 6 6 6 6 7 
7 7 7 7 7 7 7 7 7 7 7 7 7 



Solution :-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
    int i,j,k,m,n,x;
    scanf("%d",&n);
    k=n;
    m = n+(n-1);
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(i<=n-1)
            {        
                if(i==0)
                {
                printf("%d ",k);
                }
                if(i>=1)
                {
                    if(j<i)
                    {
                        printf("%d ",k-j);
                    }
                    else if(j>=i && j<m-i)
                    {
                        printf("%d ",k-i);
                    }
                    else 
                    {
                        printf("%d ",(j-k+1)+1);
                    }
                }
            }
            else if(i==n-1)
            {
                if(j<n)
                {
                    printf("%d ",k-j);
                }
                else
                {
                    printf("%d ",(j-k+1)+1);
                }
            }
            else if(i>=n)
            {
                x = m-i-1;
                if(i==m)
                {
                printf("%d ",k);
                }
                if(j<x)
                {
                    printf("%d ",k-j);
                }
                else if(j>=x && j<m-x)
                {
                    printf("%d ",k-x);
                }
                else 
                {
                    printf("%d ",(j-k+1)+1);
                }    
            }
        }
        printf("\n");
    }
}



Disclaimer :-
the whole problem statement are given by Hackerrank.com but the solution are generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
17 Comments
  • Anonymous
    Anonymous Friday, June 12, 2020

    This solution is also work

    #include
    #include
    #include
    #include

    int main()
    {
    int n;
    scanf("%d", &n);

    for(int i = n; i >= 1; i--){
    for(int j = n; j > i; j--)
    printf("%d ", j);
    for(int j = 1; j <= 2 * i - 1; j++)
    printf("%d ", i);
    for(int j = i + 1; j <= n; j++)
    printf("%d ", j);
    printf("\n");
    }

    for(int i = 1; i < n; i++){
    for(int j = n; j > i; j--)
    printf("%d ", j);
    for(int j = 1; j <= 2 * i - 1; j++)
    printf("%d ", i + 1);
    for(int j = i + 1; j <= n; j++)
    printf("%d ", j);
    printf("\n");
    }

    return 0;
    }

    • Unknown
      Unknown Saturday, August 15, 2020

      Not working

    • Ankita Upadhyay
      Ankita Upadhyay Friday, September 18, 2020

      Works thank you

  • CodeWorld19
    CodeWorld19 Saturday, August 15, 2020

    Hello everyone I have not sure about the solution which has provide by some one in comment section. follow the solution which I have given in post section
    -if any one has occurred problem then comment, otherwise fill the following contact form.
    Thank you 🙏

  • unknown
    unknown Tuesday, December 15, 2020

    This comment has been removed by the author.

  • unknown
    unknown Tuesday, December 15, 2020

    can you also explain the logic, i have a hard time solving this kind of looping patterns in c. or at least can you give me tips to solve any kind of looping patterns in c? ugh i have a bad logic

  • Unknown
    Unknown Wednesday, March 03, 2021

    #include
    #include
    #include
    #include
    int main()
    {
    int i,j,k,m,n,x;
    scanf("%d",&n); //3
    k=n; //2
    m = n+(n-1); //m=5
    for(i=0;i=1) //i=2 j=4
    {
    if(j=i && j=i j<5-i
    {
    printf("%d ",k-i);
    }
    else
    {
    printf("%d ",(j-k+1)+1);
    }
    }
    }
    else if(i>=n) //i>=3 i=4 j=2 [k=3,m=5,n=3,x=4-i]
    {
    x = m-i-1; //x=0 j=2
    if(j=x && j<m-x)
    {
    printf("%d ",k-x);
    }
    else
    {
    printf("%d ",(j-k+1)+1);
    }
    }
    }
    }
    return 0;
    }

  • Unknown
    Unknown Monday, September 20, 2021

    #include
    #include
    using namespace std;
    int main()
    {
    int n;
    cin>>n;

    for (int i = -n + 1; i < n; i++)
    {
    for (int j = -n + 1; j < n; j++)
    {
    cout<<" "<<max(abs(i), abs(j)) + 1;
    }
    cout<<"\n";
    }
    return 0;
    }

    • khaled walieed
      khaled walieed Wednesday, February 16, 2022

      how to print it

  • Unknown
    Unknown Saturday, November 06, 2021

    #include
    int main()
    {
    int n;
    scanf("%d",&n);
    int size=2*n-1;
    int start=0;
    int end=size-1;
    int a [size][size];
    while(n != 0)
    {
    for(int i=start;i<=end;i++)
    {
    for(int j=start;j<=end;j++)
    {
    if(i==start || i==end || j==start || j ==end)
    a[i][j];
    }
    }
    ++ start;
    --end;
    --n;
    }
    return 0;
    }

    • Anonymous
      Anonymous Monday, June 20, 2022

      galat h code

  • Divya
    Divya Thursday, November 11, 2021

    This comment has been removed by the author.

  • USBs & Packaging
    USBs & Packaging Tuesday, February 01, 2022

    we design promotional USBs

  • suman_cse
    suman_cse Friday, April 22, 2022

    #include
    #include
    #include
    #include

    int main()
    {

    int n,i,j,line=0,N,element=0;
    scanf("%d", &n);N=2*n-1;

    for(i=0;in-line)
    element--;

    }

    else
    {
    if(j+line>N-2)
    element++;

    }



    }printf("\n");

    if(i<N/2)
    {

    line++;

    }

    else
    {

    line--;

    }
    }


    return 0;
    }

  • KRISHNA PATHAK
    KRISHNA PATHAK Sunday, June 19, 2022

    This comment has been removed by the author.

  • KRISHNA PATHAK
    KRISHNA PATHAK Sunday, June 19, 2022

    This comment has been removed by the author.

  • Anonymous
    Anonymous Sunday, September 18, 2022

    public class bigMatrix {
    public static void main(String args[])
    {
    int n=4;
    for(int row=0;row<=n*2;row++)
    {
    int colNum=(row>n)?(2*n-row):row;
    for(int col=0;col<=2*n;col++)
    {
    if(col<=colNum )
    System.out.print(col);
    else if ((n*2-col)<=colNum)
    System.out.print(n*2-col+" hi ");
    else
    System.out.print(colNum);

    }
    System.out.println();

    }

    }
    }

Add Comment
comment url