Open In App

Square with Ascending and Descending Edges

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ code to implement the above approach.
#include <cmath>
#include <iostream>
 
using namespace std;
 
// Driver's code
int main()
{
 
    // Size of the pattern
    int n = 4;
 
    // Iterate over the rows of the pattern
    for (int i = 0; i < n; i++) {
 
        // Iterate over the columns
        // of the pattern
        for (int j = 0; j < n; j++) {
 
            // Calculate the number to be
            // placed in the current
            // position
            int num = n - abs(n - 1 - (i + j));
 
            // Print the number
            cout << num << " ";
        }
 
        // Start a new line after each row
        cout << endl;
    }
 
    return 0;
}


Java




// Java code to implement the above approach
 
public class Main {
    public static void main(String[] args)
    {
        // Size of the pattern
        int n = 4;
        // Iterate over the rows of the pattern
        for (int i = 0; i < n; i++) {
            // Iterate over the columns of the pattern
            for (int j = 0; j < n; j++) {
                // Calculate the number to be placed in the
                // current position
                int num = n - Math.abs(n - 1 - (i + j));
                // Print the number
                System.out.print(num + " ");
            }
            // Start a new line after each row
            System.out.println();
        }
    }
}
// This code is contributed by Abhijit Ghosh


Python3




# Size of the pattern
n = 4
 
# Iterate over the rows of the pattern
for i in range(n):
    # Iterate over the columns of the pattern
    for j in range(n):
        # Calculate the number to be placed in the current position
        num = n - abs(n - 1 - (i + j))
        # Print the number
        print(num, end=" ")
    # Start a new line after each row
    print()


C#




// C# code to implement the above approach.
 
using System;
 
class GFG {
    static void Main(string[] args)
    {
        // Size of the pattern
        int n = 4;
 
        // Iterate over the rows of the pattern
        for (int i = 0; i < n; i++) {
 
            // Iterate over the columns
            // of the pattern
            for (int j = 0; j < n; j++) {
                // Calculate the number to be
                // placed in the current
                // position
 
                int num = n - Math.Abs(n - 1 - (i + j));
 
                // Print the number
                Console.Write(num + " ");
            }
 
            // Start a new line after each row
            Console.WriteLine();
        }
    }
}


Javascript




// JavaScript code to implement the above approach.
 
// Size of the pattern
let n = 4;
 
// Iterate over the rows of the pattern
for (let i = 0; i < n; i++) {
 
  // Iterate over the columns
  // of the pattern
  for (let j = 0; j < n; j++) {
 
      // Calculate the number to be
      // placed in the current
      // position
      let num = n - Math.abs(n - 1 - (i + j));
 
      // Print the number
      process.stdout.write(num + " ");
  }
 
  // Start a new line after each row
  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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads