Last Updated: 27 Nov, 2020

Reversing Series Pattern

Easy

Problem statement

You are given an integer ‘N’. Your task is to return a reverse series pattern for the given ‘N’.

The reverse series pattern for ‘N’ = 5 will look like following:

1 
3 2 
4 5 6 
10 9 8 7 
11 12 13 14 15
Input Format :
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.

The first and only line of each test case contains an integer 'N' denoting the number of rows.
Output format :
For each test case print the integers corresponding to the pattern in the next ‘N’ lines.
Note
You are not required to print anything, it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 10
1 <= N <= 500

Time Limit : 1 sec

Approaches

01 Approach

For the given input ‘N’, the pattern contains ‘N’ rows with the ith row containing ‘i’ number of elements, where 1 <= ‘i’ <= ‘N’. The numbers start from 1 and increase up to ‘N’ * (N + 1) / 2. We have to return ‘N’ lines in a specific pattern where the numbers in the odd rows are in increasing order, whereas the numbers in the even rows are in decreasing order.

 

Algorithm :

 

  1. Declare the 2-D vector and push N empty vectors in it.
  2. Initialize variable ‘TEMP’ to 1.
  3. Iterate from 1 to ‘N’ (say, iterator = ‘i’).
    1. Check whether ‘i’ is odd or even.
    2. If ‘i’ is odd, iterate from 1 to ‘i’ (say, iterator = ‘j’).
      1. Push the value of ‘TEMP’ in the 2-D vector.
      2. Increment value of ‘TEMP’ by 1.
    3. Increment the value of ‘TEMP’ by ‘i’.
    4. .  If ‘i’ is even, iterate from 1 to ‘i’ (say, iterator = ‘j’).
      1. Push the value of ‘TEMP’ in the 2-D vector..
      2. Decrement value of ‘TEMP’ by 1.
    5. Increment the value of temp by ‘i’ + 1 after exiting from the inner loop.