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
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.
1 <= N <= 1000
Time Limit : 1sec
2
1 2 5 6
3 4
4
1 2 3 4 17 18 19 20
5 6 7 14 15 16
8 9 12 13
10 11
Divide the pattern into 2 parts.
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 :
O(N^2), Where N is the given input integer.
We are printing number from 1 to N*(N+1). Hence time complexity is O(N^2).
O(1).
We are using constant space to solve this.