Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Print N to 1 without loop | Normal Recursion and Backtracking
    Data Structures & Algorithms

    Print N to 1 without loop | Normal Recursion and Backtracking

    codeanddebugBy codeanddebug29 June 2025No Comments3 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Featured image of question to print N to 1 without Loops
    Share
    Facebook Twitter LinkedIn Pinterest Email

    The “Print N to 1 without loop” problem on GeeksforGeeks challenges you to output the integers from N down to 1 strictly with recursion, no for or while loops allowed.

    Here’s the [Problem Link] to begin with.

    Why is this useful?

    • Strengthens your grasp of recursive control flow.
    • Demonstrates how the position of work (before or after the recursive call) controls output order, an idea that extends to DFS, tree traversals, and backtracking algorithms.

    Example

    InputExpected Output
    N = 55 4 3 2 1

    Below are two clean recursive patterns: a straightforward top-down call and a backtracking variation.


    Simple Recursion Solution

    Intuition & Approach

    1. Base Case: Stop once num drops below 1.
    2. Work Before Recursive Call: Print num immediately, then recurse with num - 1.
    3. Because printing happens on the way down the call stack, numbers naturally appear in descending order.

    Code Implementation

    class Solution:
        def func(self, num):
            # Base Case: when num goes below 1, stop recursion
            if num < 1:
                return
            print(num, end=" ")    # ✍️ print current number
            self.func(num - 1)     # 🔁 recurse on the next smaller number
    
        def printNos(self, n):
            # Driver method that kicks off recursion
            self.func(n)

    Code Explanation

    • print(num, end=" ") executes first in every frame, emitting num as the call stack deepens.
    • The recursion depth equals N, so the call stack size grows linearly with input.

    Dry Run (N = 3)

    DepthnumPrinted So Far
    133
    223 2
    313 2 1
    40— (stop)

    Time & Space Complexity

    • Time: O(N) – one function call and one print per integer.
    • Space: O(N) – the maximum call-stack depth.

    Conclusion

    This direct version is easy to remember: “do the work first, then recurse.” It’s perfect for any pre-order output pattern.


    Backtracking Solution

    Intuition & Approach

    1. Base Case: End recursion when the index i exceeds num.
    2. Work After Recursive Call: First recurse with i + 1, then print i while backtracking.
    3. Because printing happens on the way up, we must start i at 1 and let the recursion unwind to produce num, …, 1.

    Code Implementation

    class Solution:
        def func(self, i, num):
            # Base Case: once the index surpasses num, end recursion
            if i > num:
                return
            self.func(i + 1, num)  # 🔁 dive deeper first
            print(i, end=" ")      # ✍️ print while backtracking
    
        def printNos(self, n):
            # Start from 1 so backtracking prints n down to 1
            self.func(1, n)

    Code Explanation

    • The recursive calls build the stack until i exceeds num.
    • As frames return, each prints its own i, resulting in descending output.

    Dry Run (N = 3)

    Descent Phase

    func(1,3) → func(2,3) → func(3,3) → func(4,3)  (base case)

    Backtracking Phase

    Returning FromPrintsOutput So Far
    func(3,3)33
    func(2,3)23 2
    func(1,3)13 2 1

    Time & Space Complexity

    • Time: O(N) – identical to the simple version.
    • Space: O(N) – call-stack depth remains N.

    Conclusion

    Placing the print after the recursive call turns the function into a post-order printer. This pattern is the backbone of algorithms that need to process children before parents (e.g., post-order tree traversals).


    Final Thoughts

    • Both approaches satisfy the “no loop” requirement in O(N) time.
    • The difference lies in when the work is done:
      • Simple Recursion: print on the way down.
      • Backtracking: print on the way up.
    • Understanding this placement is crucial for mastering recursion-driven algorithms across data structures and problem domains.

    Happy coding, and may your recursive calls always find their base case!

    Join our Advance DSA COURSE

    For any changes to the article, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Easy Recursion
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlePrint N times with Recursion | GFG
    Next Article Sum of first n terms | Using Recursion | Explained
    codeanddebug
    • Website

    Related Posts

    Data Structures & Algorithms

    Search in Rotated Sorted Array | Leetcode 33 | Optimal Binary Search

    3 July 2025
    Data Structures & Algorithms

    Count occurrences of a number in a sorted array with duplicates | Optimal Binary Search

    3 July 2025
    Data Structures & Algorithms

    Find First and Last Position of Element in Sorted Array | Leetcode 34 | Optimal Binary Search

    2 July 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (93)
      • Beginner (44)
      • Expert (22)
      • Intermediate (27)
    • Uncategorised (1)
    Recent Posts

    Search in Rotated Sorted Array | Leetcode 33 | Optimal Binary Search

    3 July 2025

    Count occurrences of a number in a sorted array with duplicates | Optimal Binary Search

    3 July 2025

    Find First and Last Position of Element in Sorted Array | Leetcode 34 | Optimal Binary Search

    2 July 2025

    Ceil The Floor | Binary Search Implementation

    2 July 2025

    Search Insert Position | Leetcode 35 | Explained in Python

    2 July 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.