U

4. Print Numbers 1 to N and N to 1

Easy

Problem Description

Given a positive integer NN, print all integers from 11 to NN in ascending order on the first line, and then print all integers from NN down to 11 in descending order on the second line.

Elements on each line must be separated by a single space.

Input Format

A single line containing a positive integer NN.

Output Format

  • •Line 1: Integers from 11 to NN separated by space.
  • •Line 2: Integers from NN down to 11 separated by space.
Example 1:
Input:
5
Output:
1 2 3 4 5
5 4 3 2 1
Explanation: Ascending sequence from 1 to 5, followed by descending sequence from 5 down to 1.
Example 2:
Input:
1
Output:
1
1
Explanation: When N = 1, both sequences contain only the single number 1.
Example 3:
Input:
3
Output:
1 2 3
3 2 1
Explanation: Line 1 is 1 2 3, line 2 is 3 2 1.
Constraints:
  • 1 <= N <= 10^5
  • Time complexity target: O(N)
  • Auxiliary space target: O(1)
Follow up:How can this be implemented using recursion without hitting recursion depth limits?
1,180 Online