https://www.codechef.com/TNP42020/problems/TNP403

PROBLEM LINK: CodeChef: Practical coding for everyone

Practice

Author: Setter’s name
Tester: Tester’s name
Editorialist: Editorialist’s name
DIFFICULTY : BEGINNER

PREREQUISITES:

Loops, arrays and patterns

PROBLEM:

Sherin likes to play with numbers.Sherin decides to make a pyramid using numbers.Help Sherin to make the pyramid.

QUICK EXPLANATION:

Print the pyramid according to the number of rows.

EXPLANATION:

the given pattern can be expressed as
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4

one way to view it is
1
2 3 4
3 4 5 4 3
4 5 6 7 6 5 4

The size of bold elements is increasing by 1 and the first element value is increasing by 1 each time. store this in an array and print the reverse of bold values other than last value. This is one way to solve it.

SOLUTIONS:

Setter's Solution

#include
using namespace std;

int main()
{
int rows,t, count = 0, count1 = 0, k = 0;
cin>>t;
while(t–)
{
cin >> rows;

for(int i = 1; i <= rows; ++i)
{
    for(int j = 1; j <= rows-i; ++j)
    {
    
        ++count;
    }

    while(k != 2*i-1)
    {
        if (count <= rows-1)
        {
            cout << i+k<<" ";
            ++count;
        }
        else
        {
            ++count1;
            cout << i+k-2*count1<<" ";
        }
        ++k;
    }
    count1 = count = k = 0;

    cout << endl;
}
}
return 0;

}

Tester's Solution

#include
using namespace std;

int main()
{
int rows,t, count = 0, count1 = 0, k = 0;
cin>>t;
while(t–)
{
cin >> rows;

for(int i = 1; i <= rows; ++i)
{
    for(int j = 1; j <= rows-i; ++j)
    {
    
        ++count;
    }

    while(k != 2*i-1)
    {
        if (count <= rows-1)
        {
            cout << i+k<<" ";
            ++count;
        }
        else
        {
            ++count1;
            cout << i+k-2*count1<<" ";
        }
        ++k;
    }
    count1 = count = k = 0;

    cout << endl;
}
}
return 0;

}

Editorialist's Solution

T=int(input())
while(T):
R=int(input())
k=1
B=[]
for i in range(1,R+1):
A=[]
for j in range(i,k+1):
A.append(j)
if(i!=1):
B=A[::-1]
A=A[:]+B[1:]
for _ in A:
print(_,end=" ")
print()
k+=2
T-=1