{"id":450,"date":"2025-06-29T19:24:10","date_gmt":"2025-06-29T13:54:10","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=450"},"modified":"2025-06-29T19:24:12","modified_gmt":"2025-06-29T13:54:12","slug":"print-n-times-with-recursion","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/","title":{"rendered":"Print N times with Recursion | GFG"},"content":{"rendered":"\n<p>The <strong>\u201cPrint N times with Recursion\u201d<\/strong> problem on GeeksforGeeks asks you to output the string <strong><code>\"GFG\"<\/code> exactly <code>N<\/code> times<\/strong>, using only recursive function calls (no loops).<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/print-gfg-n-times\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p>Typical interview follow-ups include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Showing a <em>straightforward<\/em> recursive call that prints on the way <strong>down<\/strong> the call stack (no backtracking).<\/li>\n\n\n\n<li>Demonstrating a <em>backtracking<\/em> version that defers printing until the stack <em>unwinds<\/em>.<\/li>\n<\/ul>\n\n\n\n<p>Both variants illustrate how the <em>position of work<\/em> in a recursive routine (before vs. after the recursive call) changes the order of operations, an essential concept for DFS, tree traversals, and more.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-f6baf2fa-337b-4a47-9bd4-4883b9885649\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#0-without-backtracking-solution\" style=\"\">Without Backtracking Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#1-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#4-dry-run-n-3-\" style=\"\">Dry Run (n = 3)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#5-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#7-with-backtracking-solution\" style=\"\">With Backtracking Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#8-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#10-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#11-dry-run-n-3-\" style=\"\">Dry Run (n = 3)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#12-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#13-conclusion\" style=\"\">Conclusion<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/print-n-times-with-recursion\/#14-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-without-backtracking-solution\">Without Backtracking Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-amp-approach\">Intuition &amp; Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Base Case:<\/strong> Stop recursion when the current index <code>i<\/code> exceeds <code>n<\/code>.<\/li>\n\n\n\n<li><strong>Work Before Recursive Call:<\/strong> Print <code>\"GFG\"<\/code> immediately, then recurse with <code>i + 1<\/code>.<\/li>\n\n\n\n<li>Because the print happens on the <em>way down<\/em>, the output appears left-to-right (<code>GFG GFG \u2026<\/code>) without delay.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def func(self, i, n):\n        # Base case: once i exceeds n, stop recursion\n        if i &gt; n:\n            return\n        print(&quot;GFG&quot;, end=&quot; &quot;)   # work done before deeper call\n        self.func(i + 1, n)     # recursive call with next index\n\n    def printGfg(self, n):\n        # Kick-off with i = 1\n        self.func(1, n)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">func<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">i<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Base case: once i exceeds n, stop recursion<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> i &gt; n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;GFG&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&quot; &quot;<\/span><span style=\"color: #D4D4D4\">)   <\/span><span style=\"color: #6A9955\"># work done before deeper call<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.func(i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, n)     <\/span><span style=\"color: #6A9955\"># recursive call with next index<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">printGfg<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Kick-off with i = 1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.func(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, n)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>print(\"GFG\", end=\" \")<\/code> executes <strong>first<\/strong>; the recursive call happens after.<\/li>\n\n\n\n<li>Each call\u2019s local variables (<code>i<\/code>, <code>n<\/code>) live on the call stack until that call returns, but printing is unaffected by the eventual unwinding.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run-n-3-\">Dry Run (<code>n = 3<\/code>)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Call Stack Depth<\/th><th><code>i<\/code><\/th><th>Printed So Far<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>1<\/td><td><strong>GFG<\/strong><\/td><\/tr><tr><td>2<\/td><td>2<\/td><td>GFG <strong>GFG<\/strong><\/td><\/tr><tr><td>3<\/td><td>3<\/td><td>GFG GFG <strong>GFG<\/strong><\/td><\/tr><tr><td>4 (base case)<\/td><td>4<\/td><td>\u2014 (stop)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(N)<\/code> \u2014 one print and one recursive call per value of <code>i<\/code>.<\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(N)<\/code> \u2014 call stack depth equals <code>N<\/code> (tail-recursion isn\u2019t optimized in Python).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h3>\n\n\n\n<p>Printing on the forward path is simple and intuitive. The order is natural, but the call stack still grows linearly with <code>N<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-with-backtracking-solution\">With Backtracking Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-amp-approach\">Intuition &amp; Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Base Case:<\/strong> End recursion when <code>i<\/code> drops below 1.<\/li>\n\n\n\n<li><strong>Work After Recursive Call:<\/strong> First recurse with <code>i \u2212 1<\/code>; after the deeper call returns, print <code>\"GFG\"<\/code>.<\/li>\n\n\n\n<li>This deferral means printing happens while <strong>backtracking<\/strong>, producing the same repeated string but emphasizing the concept of post-order work.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def func(self, i):\n        # Base case: once i is less than 1, stop recursion\n        if i &lt; 1:\n            return\n        self.func(i - 1)        # recurse first (go deeper)\n        print(&quot;GFG&quot;, end=&quot; &quot;)   # \u270d\ufe0f work done after returning\n\n    def printGfg(self, n):\n        # Kick-off with the original n\n        self.func(n)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">func<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">i<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Base case: once i is less than 1, stop recursion<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> i &lt; <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.func(i - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)        <\/span><span style=\"color: #6A9955\"># recurse first (go deeper)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;GFG&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&quot; &quot;<\/span><span style=\"color: #D4D4D4\">)   <\/span><span style=\"color: #6A9955\"># \u270d\ufe0f work done after returning<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">printGfg<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Kick-off with the original n<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.func(n)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The recursive descent builds up the stack until <code>i<\/code> hits zero.<\/li>\n\n\n\n<li>As each frame returns, its <code>print<\/code> executes\u2014so the output is still <code>\"GFG\"<\/code> repeated <code>N<\/code> times, but the work occurs during backtracking.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run-n-3-\">Dry Run (<code>n = 3<\/code>)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Descent Phase (recursive calls)<\/th><th>i<\/th><th>Action<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>3<\/td><td>call <code>func(2)<\/code><\/td><\/tr><tr><td>2<\/td><td>2<\/td><td>call <code>func(1)<\/code><\/td><\/tr><tr><td>3<\/td><td>1<\/td><td>call <code>func(0)<\/code><\/td><\/tr><tr><td>4 (base)<\/td><td>0<\/td><td>return<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Backtracking Phase (returns)<\/th><th>i now returning from<\/th><th>Printed So Far<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>1<\/td><td><strong>GFG<\/strong><\/td><\/tr><tr><td>2<\/td><td>2<\/td><td>GFG <strong>GFG<\/strong><\/td><\/tr><tr><td>3<\/td><td>3<\/td><td>GFG GFG <strong>GFG<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(N)<\/code> \u2014 identical to forward recursion.<\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(N)<\/code> \u2014 same call-stack depth.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-conclusion\">Conclusion<\/h3>\n\n\n\n<p>Moving the print statement below the recursive call highlights backtracking: the same result, but with the action delayed until the stack unwinds. This pattern is a stepping-stone to in-order\/post-order traversals in trees and graphs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-final-thoughts\">Final Thoughts<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Both techniques satisfy the requirement to <strong>print <code>\"GFG\"<\/code> exactly <code>N<\/code> times<\/strong> using recursion alone.<\/li>\n\n\n\n<li>The key lesson isn\u2019t speed\u2014both run in linear time\u2014but understanding <strong>where<\/strong> the \u201cwork\u201d sits relative to the recursive call:\n<ul class=\"wp-block-list\">\n<li><strong>Forward recursion<\/strong> (no backtracking) for pre-order-style tasks.<\/li>\n\n\n\n<li><strong>Backtracking<\/strong> for post-order patterns.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Master these tiny examples and you\u2019ll recognize the same shape in bigger recursive problems, from generating subsets to traversing binary trees. Happy recursing!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \u201cPrint N times with Recursion\u201d problem on GeeksforGeeks asks you to output the string &#8220;GFG&#8221; exactly N times, using only recursive function calls (no loops). Here&#8217;s the [Problem Link] to begin with. Typical interview follow-ups include: Both variants illustrate how the position of work in a recursive routine (before vs. after the recursive call)<\/p>\n","protected":false},"author":1,"featured_media":452,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,4],"tags":[11],"class_list":{"0":"post-450","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-recursion"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/print-N-times-with-recursion-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions\/453"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/452"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}