Square with Ascending and Descending Edges
Last Updated :
01 Nov, 2023
Given an integer n, create a square pattern of size n x n such that the edges of the pattern contain the numbers 1 to n in ascending order on the top and left edges, and n to 1 in descending order on the bottom and right edges. The remaining elements of the pattern should be filled with numbers that are equal to the absolute difference between their row and column index plus 2. The resulting pattern should be returned as a 2 – dimensional list.
Examples:
Input: 3
Output:
1 2 3
2 3 2
3 2 1
Input: 4
Output:
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1
Approach: Follow the steps below to solve the problem:
- Take the input n, the size of the square pattern
- Initialize two variables, i and j, to track the current row and column of the pattern.
- Use two nested loops to iterate over the rows and columns of the pattern.
- In the inner loop, check the current row and column values to determine the number to be placed in that position. If i + j is equal to n – 1, the number should be n, otherwise, it should be n – abs(n – 1 – (i + j)).
- After each inner loop iteration, increment j and reset it to 0 when it reaches n
- After each outer loop iteration, increment i and reset it to 0 when it reaches n
- Repeat steps 4 to 6 until all the numbers have been placed in the pattern
- Print the pattern, with each row on a new line
Below is the implementation for the above approach:
C++14
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int n = 4;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
int num = n - abs (n - 1 - (i + j));
cout << num << " " ;
}
cout << endl;
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args)
{
int n = 4 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
int num = n - Math.abs(n - 1 - (i + j));
System.out.print(num + " " );
}
System.out.println();
}
}
}
|
Python3
n = 4
for i in range (n):
for j in range (n):
num = n - abs (n - 1 - (i + j))
print (num, end = " " )
print ()
|
C#
using System;
class GFG {
static void Main( string [] args)
{
int n = 4;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
int num = n - Math.Abs(n - 1 - (i + j));
Console.Write(num + " " );
}
Console.WriteLine();
}
}
}
|
Javascript
let n = 4;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
let num = n - Math.abs(n - 1 - (i + j));
process.stdout.write(num + " " );
}
process.stdout.write( "\n" );
}
|
Output
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1
Time complexity: O(n^2), where n is the size of the pattern. This is because the program uses two nested for loops to iterate over the n rows and n columns of the pattern, resulting in a total of n^2 iterations.
Auxiliary Space: O(1), because it only uses a few variables to store intermediate values and the memory usage does not depend on the size of the input. The variables used in the program are n, i, and j all of which have a constant size regardless of the size of the input.
Share your thoughts in the comments
Please Login to comment...