CE20201E Editorial

PROBLEM LINK:

contest: https://www.codechef.com/CE12020/problems/CE20201E
practice: https://www.codechef.com//CE20201E

Author: nash_057
Tester: shanvid
Editorialist: shanvid](https://www.codechef.com/users/shanvid)

DIFFICULTY:

Easy

PREREQUISITES:

Maths, Matrix

PROBLEM:

Form a square matrix whose border is made of numbers, in such a manner that the border is made of a single number. Start from number 1 in the outer border and increase the number as the border converges. Each Matrix with Size S will be a S x S Matrix.

EXPLANATION:

Eg. If size is 5,

Output should be:

  1    1   1   1   1 
  1    2   2   2   1
  1    2   3   2   1
  1    2   2   2   1
  1    1   1   1   1

Or if size Is 4:

  1    1   1   1
  1    2   2   1
  1    2   2   1
  1    1   1   1

Input:

  • First line will contain SS, size of matrix.

Output:

Output is a space separated matrix

Constraints

  • 3ā‰¤Sā‰¤1000

SOLUTIONS:

#include

using namespace std;

main()
{
int i, j,n,k;
cout<<"\n Enter the sie of matrix";
cin>>n;
k=0;
int a[n][n];
while(k<=n/2)
{
for(i=k;i<n-k;++i)
{
for(j=k;j<n-k;++j)
{
if(i==k || j==k || (i==n-k-1) || (j==n-k-1))
a[i][j]=k+1;
}
}
++k;
}
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
cout<<a[i][j]<<" ā€œ;
}
cout<<ā€\n";
}

}