|
U
Pro4. Print Numbers 1 to N and N to 1
Easy
Problem Description
Given a positive integer N, print all integers from 1 to N in ascending order on the first line, and then print all integers from N down to 1 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 N.
Output Format
- •Line 1: Integers from 1 to N separated by space.
- •Line 2: Integers from N down to 1 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^5Time 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
Code
|Loading Code Editor...
SavedLn 1, Col 1
input =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit