PATTERNP-editorial

PROBLEM LINK:

Practice
Contest

Author: Setter’s name
Editorialist: Editorialist’s name

DIFFICULTY:

SIMPLE-EASY

PROBLEM:

Identify the logic behind the series 6,20,47,87,140, …… and print a pattern using these numbers as described below.
The number of rows in the pattern is specified by the input N.
EXAMPLE:
If N=3 then the pattern will be:
00006
00020 00047
00087 00140 00206

QUICK EXPLANATION:

The problem can be solved using simple nested loops structure.

EXPLANATION:

Input the number of test cases,the number of inputs in each test case and within it the value of N(no. of rows).
The series starts with 6 and 20 and continues following a certain logic.
The logic for it is as follows:
Suppose a=6 and b=20 ,then the difference between the two numbers is 14 .Moving on to the next number 47,find the difference between 47 and 20.The difference obtained is 27.
Next, the difference between 47 and 87 is equal to 40 and so on the differences are calculated.Finding the differences we notice that the when a value 13 is added to the difference between first two numbers
(6 and 20 or any other number),the difference between 20 and 47 is obtained. Similarly on adding 13 to the difference between 20 and 47 ,a number is obtained which is the difference between 47 and 87 and so on.
This is how the series continues.
Hence, to find the next number(starting from 3) as first number=6 and second number=20, we use the logic as described below:
next=(secondnumber*2)-firstnumber+13;
firstnumber=secondnumber
secondnumber=next
This is how we will obtain the next number and we will continue printing the pattern till the value N(number of rows).
For the first row we need to print 1 value,for second row we will print 2 values, for third 3 values and so on.

For the format ‘00006 ,00020,00087…’ ,there is no such logic for it.It is simply done by formatting of outputs which has a different syntax in different programming languages.

SOLUTIONS:

Setter's Solution

#include
using namespace std;
int element(int);
int main()
{ int t;
cin>>t;
while(t–){
int k,n,i;
cin>>k;
while(k–)
{
i=1;
cin>>n;
for(int row=1;row<=n;row++)
{

        for(int ele=1;ele<=row;ele++)
        {
            printf("%05d",element(i));
            cout<<" ";
            i=i+1;
        }
        cout<<endl;
    }
    
}}

}
int element(int i)
{
int first=6,second=20,next;
if(i==1)
return first;
else if(i==2)
return second;
else
{
for(int j=3;j<=i;j++)
{
next=(second*2)-first+13;
first=second;
second=next;
}
return next;
}

}

Editorialist's Solution

#include
using namespace std;
int element(int);
int main()
{ int t;
cin>>t;
while(t–){
int k,n,i;
cin>>k;
while(k–)
{
i=1;
cin>>n;
for(int row=1;row<=n;row++)
{

        for(int ele=1;ele<=row;ele++)
        {
            printf("%05d",element(i));
            cout<<" ";
            i=i+1;
        }
        cout<<endl;
    }
    
}}

}
int element(int i)
{
int first=6,second=20,next;
if(i==1)
return first;
else if(i==2)
return second;
else
{
for(int j=3;j<=i;j++)
{
next=(second*2)-first+13;
first=second;
second=next;
}
return next;
}

}