medium
0 views

Series – Decreasing Limits

Print integers in decreasing limits from N to 1, then N-1 to 2, then N-2 to 3, and so on until the limits meet.

Understand the Problem

Problem Statement

Series – Decreasing Limits: The program must accept an integer N as the input. The program must print the integers from N to 1. Then the program must print the integers from N-1 to 2. Then the program must print the integers from N-2 to 3. Similarly, the program must print the integers in the remaining decreasing limits.

Constraints

  • 1 ≤ N ≤ 500
  • Output must be space-separated integers
  • Each decreasing limit must be printed in sequence

Examples

Example 1
Input
6
Output
6 5 4 3 2 1 5 4 3 2 4 3
Explanation

First prints 6 to 1 (6 5 4 3 2 1), then 5 to 2 (5 4 3 2), then 4 to 3 (4 3). The next would be 3 to 4, but since 3 < 4, the sequence stops.

Example 2
Input
5
Output
5 4 3 2 1 4 3 2 3
Explanation

First prints 5 to 1 (5 4 3 2 1), then 4 to 2 (4 3 2), then 3 to 3 (3). The next would be 2 to 4, but since 2 < 4, the sequence stops.

Solution

#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    
    int start = n;
    int end = 1;
    
    while (start >= end) {
        for (int i = start; i >= end; i--) {
            printf("%d ", i);
        }
        start--;
        end++;
    }
    
    return 0;
}
Time:O(N²) - The outer while loop runs approximately N/2 times, and the inner for loop prints decreasing sequences of length N, N-2, N-4, etc.
Space:O(1) - Only uses a constant amount of extra space for variables
Approach:

Reads integer N from standard input. Uses two variables: start (initialized to N) and end (initialized to 1). The while loop continues while start >= end. Inside the loop, a for loop prints integers from start down to end. After each complete sequence, start is decremented and end is incremented to create the next decreasing limit. The process continues until start becomes less than end.

Visual Explanation

Loading diagram...