Number Pattern

Posted: 17 Mar, 2021
Difficulty: Easy

PROBLEM STATEMENT

Try Problem

Write a program such that you are given an integer ‘N’ and print the pattern consisting of ‘N’ lines.

For Example :
If we are given N=3
The pattern is :
1 2 3 10 11 12 
4 5 8 9 
6 7 
Input Format :
The first and only line contains a single integer ‘N’.
Output Format :
Return the 2D list such that the first list contains elements in the first row, the second list contains elements in the second row and so on.
Constraints :
1 <= N <= 1000

Time Limit : 1sec
Approach 1

Explanation:- The main idea is to divide the patterns into 2 parts. For example:-

N=4

LHS:

1 2 3 4

5 6 7

8 9

10

RHS:

17 18 19 20

14 15 16

12 13

11

In the LHS part, counting is from 1 to N* (N+1)/2.

In RHS part,counting is from (N * (N+1)/2) + 1 to N * (N+1) but from downwards to upwards.

Algorithm :

  • Run a loop from i=0 to N.
  • First, print the pattern from (i-1) * N+1 to (i-1) * N + N - (i-1).
  • Then print the pattern of reverse numbering.
Try Problem