{"id":61,"date":"2025-05-09T13:09:59","date_gmt":"2025-05-09T07:39:59","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=61"},"modified":"2025-07-07T19:58:15","modified_gmt":"2025-07-07T14:28:15","slug":"reverse-array-using-recursion-and-while-loop","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/","title":{"rendered":"Python Program to Reverse Array Using Recursion and While Loop"},"content":{"rendered":"\n<p>Hi everyone! In this article, we&#8217;ll guide you through the Python program to reverse an array using recursion and while loop. So, let&#8217;s get started with the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/reverse-sub-array5620\/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>].<\/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-30393d50-b185-43e0-ab1f-87f8675c059b\" 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\">Content:<\/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\/reverse-array-using-recursion-and-while-loop\/#0-examples-of-the-python-program-to-reverse-array-using-recursion-and-while-loop\" style=\"\">Examples of the Python Program to Reverse Array Using Recursion and While Loop:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#1-points-to-keep-in-mind\" style=\"\">Points to Keep in Mind:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#2-1recursion-solution\" style=\"\">1.RECURSION SOLUTION<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#3-problem-statement\" style=\"\">Problem Statement:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#4-intuition-and-approach\" style=\"\">Intuition and Approach:<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#5-key-insights\" style=\"\">Key Insights:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#6-approach-overview-\" style=\"\">Approach Overview:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#7-why-this-works\" style=\"\">Why This Works:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#8-code\" style=\"\">Code:<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#9-highlights\" style=\"\">Highlights:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#10-dry-run\" style=\"\">Dry Run:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#11-edge-cases\" style=\"\">Edge Cases:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#12-time-and-space-complexity\" style=\"\">Time and Space Complexity:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#13-2while-loop-solution\" style=\"\">2.WHILE LOOP SOLUTION<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#14-problem-statement\" style=\"\">Problem Statement:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#15-intuition-and-approach\" style=\"\">Intuition and Approach:<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#16-key-insights\" style=\"\">Key Insights:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#17-step-by-step-approach\" style=\"\">Step-by-Step Approach:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#18-why-this-works\" style=\"\">Why This Works:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#19-code\" style=\"\">Code:<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#20-components-of-the-code\" style=\"\">Components of the Code:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#21-assumptions-and-clarifications\" style=\"\">Assumptions and Clarifications:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#22-dry-run\" style=\"\">Dry Run:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#23-edge-cases\" style=\"\">Edge Cases:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/reverse-array-using-recursion-and-while-loop\/#24-time-and-space-complexity\" style=\"\">Time and Space Complexity:<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-examples-of-the-python-program-to-reverse-array-using-recursion-and-while-loop\">Examples of the Python Program to Reverse Array Using Recursion and While Loop:<\/h2>\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=\"Example 1:\n\n  Input: arr[] = [1, 2, 3, 4, 5, 6, 7] and L = 2 and R = 4\n  Output: [1, 4, 3, 2, 5, 6, 7]\n  Explanation: After reversing the elements in range 2 to 4 (2, 3, 4), modified array is 1, 4, 3, 2, 5, 6, 7.\n\nExample 2:\n\n  Input: arr[] = [1, 6, 7, 4] and L = 1 and R = 4\n  Output: [4, 7, 6, 1]\n  Explanation: After reversing the elements in range 1 to 4 (1, 6, 7, 4), modified array is 4, 7, 6, 1.\" 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: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr[] = [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">] <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> L = <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> R = <\/span><span style=\"color: #B5CEA8\">4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: After reversing the elements <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> to <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">), modified array <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr[] = [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">] <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> L = <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> R = <\/span><span style=\"color: #B5CEA8\">4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: After reversing the elements <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> to <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">), modified array <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">6<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-points-to-keep-in-mind\">Points to Keep in Mind:<\/h2>\n\n\n\n<p><strong>Expected Time Complexity: O(n)<\/strong><\/p>\n\n\n\n<p><strong>Expected Auxiliary Space: O(1)<\/strong><\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 \u2264 <strong>arr.size()<\/strong> \u2264 10<sup>6<\/sup><\/li>\n\n\n\n<li>1 \u2264 <strong>arr[i]<\/strong> \u2264 10<sup>6<\/sup><\/li>\n\n\n\n<li>1 \u2264 <strong>L \u2264 R<\/strong> \u2264 arr.size()<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-1recursion-solution\">1.RECURSION SOLUTION<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-problem-statement\">Problem Statement:<\/h3>\n\n\n\n<p><strong>Given:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An array of integers <strong>&#8220;arr&#8221;<\/strong><\/li>\n\n\n\n<li>Two integers <strong>&#8220;l&#8221;<\/strong> and <strong>&#8220;r&#8221;,<\/strong> representing the starting and ending indices (1-based) of a subarray within <strong>&#8220;arr&#8221;<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>Task:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reverse the elements of the subarray from index <strong>&#8220;l&#8221;<\/strong> to index <strong>&#8220;r&#8221; <\/strong>in-place<\/li>\n\n\n\n<li>Return the modified array after the reversal<\/li>\n<\/ul>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 &lt;= <strong>l<\/strong> &lt;= <strong>r <\/strong>&lt;= len(arr)<\/li>\n\n\n\n<li>1 &lt;= <strong>len(arr)<\/strong> &lt;= 10^5<\/li>\n\n\n\n<li>-10^9 &lt;= <strong>arr[i]<\/strong> &lt;= 10^9<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\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=\"Example 1:\n\n  Input: arr = [1, 2, 3, 4, 5], l = 2, r = 4\n  Output: [1, 4, 3, 2, 5]\n  Explanation: The subarray from index 2 to 4 is [2, 3, 4]. Reversing it results in [4, 3, 2], so the entire array becomes [1, 4, 3, 2, 5].\n\nExample 2:\n\n  Input: arr = [10, 20, 30, 40, 50], l = 1, r = 5\n  Output: [50, 40, 30, 20, 10]\n  Explanation: Reversing the entire array.\n\nExample 3:\n\n  Input: arr = [5, 4, 3, 2, 1], l = 3, r = 3\n  Output: [5, 4, 3, 2, 1]\n  Explanation: Reversing a single element doesn't change the array.\" 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: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr = [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">], l = <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, r = <\/span><span style=\"color: #B5CEA8\">4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: The subarray <\/span><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> index <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> to <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> [<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">]. Reversing it results <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> [<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">], so the entire array becomes [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">].<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr = [<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">40<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\">], l = <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, r = <\/span><span style=\"color: #B5CEA8\">5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">40<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: Reversing the entire array.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr = [<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">], l = <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, r = <\/span><span style=\"color: #B5CEA8\">3<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: Reversing a single element doesn<\/span><span style=\"color: #CE9178\">&#39;t change the array.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-intuition-and-approach\">Intuition and Approach:<\/h3>\n\n\n\n<p><strong>Objective:<\/strong> Efficiently reverse a specified portion of an array in-place without using additional memory for another array.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"5-key-insights\">Key Insights:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Two-Pointer Technique:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Utilize two pointers starting at the beginning (left) and end (right) of the subarray<\/li>\n\n\n\n<li>Swap the elements at these pointers and move them towards each other until they meet or cross<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursive Reversal:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Implement the two-pointer swapping recursively<\/li>\n\n\n\n<li><strong>Base Case:<\/strong> If left is greater than or equal to right, terminate the recursion<\/li>\n\n\n\n<li><strong>Recursive Case:<\/strong> Swap the elements at left and right, then recursively call the function with left + 1 and right &#8211; 1<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Index Adjustment:<\/strong> Convert the 1-based indices &#8220;l&#8221; and &#8220;r&#8221; to 0-based indices for array manipulation<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"6-approach-overview-\"><strong>Approach Overview:<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Index Conversion:<\/strong> Adjust the provided 1-based indices &#8220;l&#8221; and &#8220;r&#8221; to 0-based indices suitable for Python lists<\/li>\n\n\n\n<li><strong>Recursive Function (<\/strong><strong>func<\/strong><strong>):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Swap elements at the left and right positions<\/li>\n\n\n\n<li>Recursively call itself with updated indices to continue swapping inward elements<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Wrapper Function (<\/strong><strong>reverseSubArray<\/strong><strong>):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Serve as an interface that initiates the recursive reversal<\/li>\n\n\n\n<li>Return the modified array after the reversal process<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"7-why-this-works\">Why This Works:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Efficiency:<\/strong> The recursive approach ensures that each pair of elements is swapped exactly once, leading to a time complexity of O(n), where &#8220;n&#8221; is the number of elements in the subarray.<\/li>\n\n\n\n<li><strong>In-Place Modification: <\/strong>By swapping elements within the original array, the approach maintains constant space complexity (O(1)), as no additional data structures are required.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-code\">Code:<\/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, arr, left, right):\n        if left &gt;= right:\n            return\n        arr[left], arr[right] = arr[right], arr[left]\n        self.func(arr, left + 1, right - 1)\n\n    def reverseSubArray(self, arr, l, r):\n        self.func(arr, l - 1, r - 1)\n        return arr\" 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\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">left<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">right<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> left &gt;= right:<\/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\">        arr[left], arr[right] = arr[right], arr[left]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.func(arr, left + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, right - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/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\">reverseSubArray<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.func(arr, l - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, r - <\/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 style=\"color: #D4D4D4\"> arr<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The solution consists of two primary functions:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>func<\/strong><strong> Function:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Purpose<\/strong>: Recursively swaps elements in the array to reverse the specified subarray<\/li>\n\n\n\n<li><strong>Mechanism:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Base Case: <\/strong>If the left index is greater than or equal to the right index, the recursion terminates as all necessary swaps have been completed.<\/li>\n\n\n\n<li><strong>Recursive Case:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Swap the elements at the left and right indices.<\/li>\n\n\n\n<li>Recursively call &#8220;func&#8221; with incremented left and decremented right to continue swapping the next pair inward.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>reverseSubArray<\/strong><strong> Function:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Purpose:<\/strong> Acts as a wrapper that prepares the indices and initiates the recursive reversal<\/li>\n\n\n\n<li><strong>Mechanism:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Convert the 1-based indices &#8220;l&#8221; and &#8220;r&#8221; to 0-based indices by subtracting 1<\/li>\n\n\n\n<li>Call the &#8220;func&#8221; function with the adjusted indices to perform the reversal<\/li>\n\n\n\n<li>Return the modified array after the reversal is complete<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"9-highlights\">Highlights:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Recursion Depth: <\/strong>The depth of recursion corresponds to half the length of the subarray, ensuring that each pair is swapped once.<\/li>\n\n\n\n<li><strong>In-Place Swapping: <\/strong>By swapping elements directly within the array, the algorithm avoids the need for additional memory, maintaining space efficiency.<\/li>\n\n\n\n<li><strong>Index Handling: <\/strong>Adjusting indices from 1-based to 0-based ensures correct element access within the Python list.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-dry-run\">Dry Run:<\/h3>\n\n\n\n<p>Let&#8217;s walk through an example to understand how the code operates step by step.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [1, 2, 3, 4, 5]<\/li>\n\n\n\n<li>l = 2<\/li>\n\n\n\n<li>r = 4<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [1, 4, 3, 2, 5]<\/li>\n<\/ul>\n\n\n\n<p><strong>Process:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initial Setup:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Convert l and r to 0-based indices:\n<ul class=\"wp-block-list\">\n<li>l-1 = 1<\/li>\n\n\n\n<li>r-1 = 3<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Array before reversal: [1, 2, 3, 4, 5]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>First Call to <\/strong><strong>func(arr, 1, 3)<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Check Base Case:<\/strong><strong><br><\/strong>1 &lt; 3 \u2192 Proceed.<\/li>\n\n\n\n<li><strong>Swap Elements at Indices 1 and 3:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Swap arr[1] (2) with arr[3] (4).<\/li>\n\n\n\n<li>Array after swap: [1, 4, 3, 2, 5]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursive Call:<br><\/strong>func(arr, 2, 2)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Second Call to <\/strong><strong>func(arr, 2, 2)<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Check Base Case:<\/strong><strong><br><\/strong>2 &gt;= 2 \u2192 Terminate recursion.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Final Array After Reversal:<\/strong><strong><br><\/strong>[1, 4, 3, 2, 5]<\/li>\n<\/ol>\n\n\n\n<p><strong>Result: <\/strong>The subarray from index 2 to 4 has been successfully reversed.<\/p>\n\n\n\n<p><strong>Another Example:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [10, 20, 30, 40, 50, 60]<\/li>\n\n\n\n<li>l = 1<\/li>\n\n\n\n<li>r = 6<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [60, 50, 40, 30, 20, 10]<\/li>\n<\/ul>\n\n\n\n<p><strong>Process:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initial Setup:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Convert l and r to 0-based indices:\n<ul class=\"wp-block-list\">\n<li>l-1 = 0<\/li>\n\n\n\n<li>r-1 = 5<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Array before reversal: [10, 20, 30, 40, 50, 60]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>First Call to <\/strong><strong>func(arr, 0, 5)<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Check Base Case: <\/strong>0 &lt; 5 \u2192 Proceed.<\/li>\n\n\n\n<li><strong>Swap Elements at Indices 0 and 5:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Swap arr[0] (10) with arr[5] (60).<\/li>\n\n\n\n<li>Array after swap: [60, 20, 30, 40, 50, 10]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursive Call: <\/strong>func(arr, 1, 4)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Second Call to <\/strong><strong>func(arr, 1, 4)<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Check Base Case: <\/strong>1 &lt; 4 \u2192 Proceed.<\/li>\n\n\n\n<li><strong>Swap Elements at Indices 1 and 4:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Swap arr[1] (20) with arr[4] (50).<\/li>\n\n\n\n<li>Array after swap: [60, 50, 30, 40, 20, 10]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursive Call: <\/strong>func(arr, 2, 3)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Third Call to <\/strong><strong>func(arr, 2, 3)<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Check Base Case: <\/strong>2 &lt; 3 \u2192 Proceed.<\/li>\n\n\n\n<li><strong>Swap Elements at Indices 2 and 3:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Swap arr[2] (30) with arr[3] (40).<\/li>\n\n\n\n<li>Array after swap: [60, 50, 40, 30, 20, 10]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Recursive Call: <\/strong>func(arr, 3, 2)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Fourth Call to <\/strong><strong>func(arr, 3, 2)<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Check Base Case: <\/strong>3 &gt;= 2 \u2192 Terminate recursion.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Final Array After Reversal: <\/strong>[60, 50, 40, 30, 20, 10]<\/li>\n<\/ol>\n\n\n\n<p><strong>Result: <\/strong>The entire array has been successfully reversed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-edge-cases\">Edge Cases:<\/h3>\n\n\n\n<p>Handling edge cases ensures that the function behaves correctly under all possible scenarios.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Subarray of Length 1:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [5, 6, 7, 8, 9]<\/li>\n\n\n\n<li>l = 3<\/li>\n\n\n\n<li>r = 3<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [5, 6, 7, 8, 9]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing a subarray of length 1 doesn&#8217;t change the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Entire Array Reversal:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [1, 2, 3, 4]<\/li>\n\n\n\n<li>l = 1<\/li>\n\n\n\n<li>r = 4<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [4, 3, 2, 1]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing the entire array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Subarray at the Beginning:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [10, 20, 30, 40, 50]<\/li>\n\n\n\n<li>l = 1<\/li>\n\n\n\n<li>r = 3<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [30, 20, 10, 40, 50]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing the first three elements.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Subarray at the End:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [100, 200, 300, 400]<\/li>\n\n\n\n<li>l = 3<\/li>\n\n\n\n<li>r = 4<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [100, 200, 400, 300]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing the last two elements.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Array with All Identical Elements:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [7, 7, 7, 7, 7]<\/li>\n\n\n\n<li>l = 2<\/li>\n\n\n\n<li>r = 4<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [7, 7, 7, 7, 7]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing doesn&#8217;t change the array as all elements are identical.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Invalid Indices (Assuming Input Validation):<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [1, 2, 3]<\/li>\n\n\n\n<li>l = 4<\/li>\n\n\n\n<li>r = 5<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Behavior:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The function assumes valid input as per constraints.<\/li>\n\n\n\n<li>If implemented with input validation, it should handle or reject such cases gracefully.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-time-and-space-complexity\">Time and Space Complexity:<\/h3>\n\n\n\n<p><strong>Time Complexity:<\/strong> O(n)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explanation:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Reversal Function (<\/strong><strong>func<\/strong><strong>):<\/strong>\n<ul class=\"wp-block-list\">\n<li>The function recursively swaps elements from the ends towards the center.<\/li>\n\n\n\n<li>It makes approximately (r\u2212l+1)\/2 recursive calls, where n = r \u2212 l + 1 is the length of the subarray.<\/li>\n\n\n\n<li>Each call performs a constant amount of work (swapping two elements).<\/li>\n\n\n\n<li><strong>Overall:<\/strong> Linear time relative to the size of the subarray.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Wrapper Function (reverseSubArray):<\/strong> Initiates the reversal process with O(1) additional operations.<\/li>\n\n\n\n<li><strong>Combined: <\/strong>The total time complexity is O(n), where n is the number of elements in the subarray being reversed.<\/li>\n<\/ul>\n\n\n\n<p><strong>Space Complexity:<\/strong> O(n) due to Recursion<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explanation:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Recursive Calls:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Each recursive call adds a new frame to the call stack.<\/li>\n\n\n\n<li>For a subarray of size n, there are n\/2\u200b recursive calls.<\/li>\n\n\n\n<li><strong>Space Used:<\/strong> O(n) due to recursion depth.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Auxiliary Space:<\/strong> No additional data structures are used; only a few variables for swapping.<\/li>\n\n\n\n<li><strong>Overall:<\/strong> The space complexity is linear, O(n), primarily due to the recursive stack.<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-white-color has-vivid-purple-background-color has-text-color has-background has-link-color wp-elements-97b545e6a26eefc7f4dd11fe179e698d\"><em><strong>Note:<\/strong> For large subarrays, deep recursion may lead to stack overflow errors in environments with limited recursion depth. An iterative approach can achieve O(1) space complexity by eliminating recursion.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"13-2while-loop-solution\">2.WHILE LOOP SOLUTION<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-problem-statement\">Problem Statement:<\/h3>\n\n\n\n<p><strong>Given:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An array of integers &#8220;arr&#8221;<\/li>\n\n\n\n<li>Two integers &#8220;l&#8221; and &#8220;r&#8221;, representing the starting and ending positions (1-based indices) of a subarray within &#8220;arr&#8221;<\/li>\n<\/ul>\n\n\n\n<p><strong>Task:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reverse the elements of the subarray from position &#8220;l&#8221; to position &#8220;r&#8221; <strong>in-place<\/strong><\/li>\n\n\n\n<li>Return the modified array after performing the reversal<\/li>\n<\/ul>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 &lt;= l &lt;= r &lt;= len(arr)<\/li>\n\n\n\n<li>1 &lt;= len(arr) &lt;= 10^5<\/li>\n\n\n\n<li>-10^9 &lt;= arr[i] &lt;= 10^9<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\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=\"Example 1:\n\n  Input: arr = [1, 2, 3, 4, 5], l = 2, r = 4\n  Output: [1, 4, 3, 2, 5]\n  Explanation: The subarray from index 2 to 4 is [2, 3, 4]. Reversing it results in [4, 3, 2], so the entire array becomes [1, 4, 3, 2, 5].\n\nExample 2:\n\n  Input: arr = [10, 20, 30, 40, 50, 60], l = 1, r = 6\n  Output: [60, 50, 40, 30, 20, 10]\n  Explanation: Reversing the entire array.\n\nExample 3:\n\n  Input: arr = [5, 4, 3, 2, 1], l = 3, r = 3\n  Output: [5, 4, 3, 2, 1]\n  Explanation: Reversing a single element doesn't change the array.\" 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: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr = [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">], l = <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, r = <\/span><span style=\"color: #B5CEA8\">4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: The subarray <\/span><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> index <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> to <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> [<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">]. Reversing it results <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> [<\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">], so the entire array becomes [<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">].<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr = [<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">40<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\">], l = <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, r = <\/span><span style=\"color: #B5CEA8\">6<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">40<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: Reversing the entire array.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: arr = [<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">], l = <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, r = <\/span><span style=\"color: #B5CEA8\">3<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: [<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Explanation: Reversing a single element doesn<\/span><span style=\"color: #CE9178\">&#39;t change the array.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-intuition-and-approach\">Intuition and Approach:<\/h3>\n\n\n\n<p><strong>Objective:<\/strong> Efficiently reverse a specified portion of an array in-place without using additional memory for another array.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"16-key-insights\">Key Insights:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Two-Pointer Technique:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Utilize two pointers, left and right, starting at the beginning (l-1) and end (r-1) of the subarray respectively<\/li>\n\n\n\n<li>Swap the elements at these pointers<\/li>\n\n\n\n<li>Move left forward and right backward, repeating the process until left is no longer less than right<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>In-Place Reversal:<\/strong> By swapping elements within the original array, we avoid the need for extra space, ensuring space efficiency.<\/li>\n\n\n\n<li><strong>1-Based to 0-Based Indexing:<\/strong> Since array indices in most programming languages (including Python) are 0-based, adjust the given 1-based indices &#8220;l&#8221; and &#8220;r&#8221; by subtracting 1 to align with the array&#8217;s indexing.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"17-step-by-step-approach\">Step-by-Step Approach:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Index Adjustment:<\/strong> Convert the 1-based indices l and r to 0-based indices by computing left = l &#8211; 1 and right = r &#8211; 1.<\/li>\n\n\n\n<li><strong>Initialize Pointers:<\/strong> Set left to l &#8211; 1 and right to r &#8211; 1.<\/li>\n\n\n\n<li><strong>Iterative Swapping:<\/strong>\n<ul class=\"wp-block-list\">\n<li>While left &lt; right:\n<ul class=\"wp-block-list\">\n<li>Swap arr[left] with arr[right].<\/li>\n\n\n\n<li>Increment left by 1.<\/li>\n\n\n\n<li>Decrement right by 1.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Termination:<\/strong> The loop terminates when left is no longer less than right, indicating that the subarray has been fully reversed.<\/li>\n\n\n\n<li><strong>Return the Modified Array:<\/strong> After the reversal, return the updated array &#8220;arr&#8221;<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"18-why-this-works\">Why This Works:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Efficiency:<\/strong> The two-pointer technique ensures that each element in the subarray is visited only once, resulting in a time complexity of O(n), where n=r\u2212l+1 is the length of the subarray.<\/li>\n\n\n\n<li><strong>Space Optimization:<\/strong> By performing swaps in-place, the algorithm maintains a constant space complexity of O(1), as no additional data structures are required.<\/li>\n\n\n\n<li><strong>Simplicity:<\/strong> The approach is straightforward and easy to implement, reducing the likelihood of errors.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"19-code\">Code:<\/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 reverseSubArray(self, arr, l, r):\n        while left &lt; right:\n            arr[left], arr[right] = arr[right], arr[left]\n            left += 1\n            right -= 1\n        return arr\" 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\">reverseSubArray<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">l<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">r<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> left &lt; right:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr[left], arr[right] = arr[right], arr[left]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            left += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            right -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> arr<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"20-components-of-the-code\">Components of the Code:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function Definition:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Function Name:<\/strong> reverseSubArray<\/li>\n\n\n\n<li><strong>Parameters:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr: The original array of integers.<\/li>\n\n\n\n<li>l: The starting position (1-based index) of the subarray to be reversed.<\/li>\n\n\n\n<li>r: The ending position (1-based index) of the subarray to be reversed.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return Value:<\/strong> The modified array after reversing the specified subarray.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Loop Structure:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The function uses a while loop that continues as long as left &lt; right.<\/li>\n\n\n\n<li><strong>Purpose:<\/strong> To iterate over the subarray from both ends towards the center, swapping elements to achieve the reversal.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Swapping Elements:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr[left], arr[right] = arr[right], arr[left]<\/li>\n\n\n\n<li><strong>Action:<\/strong> Swaps the elements at positions left and right.<\/li>\n\n\n\n<li><strong>Result:<\/strong> Moves the element from the end to the beginning and vice versa within the subarray.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Pointer Adjustment:<\/strong>\n<ul class=\"wp-block-list\">\n<li>left += 1: Moves the left pointer one step to the right.<\/li>\n\n\n\n<li>right -= 1: Moves the right pointer one step to the left.<\/li>\n\n\n\n<li><strong>Purpose:<\/strong> Continues the traversal towards the center of the subarray after each swap.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return Statement:<\/strong>\n<ul class=\"wp-block-list\">\n<li>return arr<\/li>\n\n\n\n<li><strong>Action:<\/strong> Returns the modified array after completing the reversal process.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"21-assumptions-and-clarifications\">Assumptions and Clarifications:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Index Initialization:<\/strong> The variables left and right should be initialized before entering the while loop. Based on the problem statement and typical implementation, it&#8217;s implied that left = l &#8211; 1 and right = r &#8211; 1 to convert 1-based indices to 0-based.<\/li>\n\n\n\n<li><strong>Correctness of Indices:<\/strong> The function assumes that the provided indices l and r are valid (i.e., 1 &lt;= l &lt;= r &lt;= len(arr)). Handling of invalid indices is not included and should be managed externally or by incorporating additional checks.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22-dry-run\">Dry Run:<\/h3>\n\n\n\n<p>To illustrate how the &#8220;reverseSubArray&#8221; function operates, let&#8217;s walk through an example step by step<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-1024x576.png\" alt=\"\" class=\"wp-image-62\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-1.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-1024x576.png\" alt=\"\" class=\"wp-image-63\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/revere-an-array-2.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"23-edge-cases\">Edge Cases:<\/h3>\n\n\n\n<p>Ensuring that the reverseSubArray function handles all possible scenarios correctly is vital for its robustness. Here are some noteworthy edge cases:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Subarray of Length 1:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [5, 6, 7, 8, 9]<\/li>\n\n\n\n<li>l = 3<\/li>\n\n\n\n<li>r = 3<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [5, 6, 7, 8, 9]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing a subarray that contains only one element (7) doesn&#8217;t change the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Entire Array Reversal:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [1, 2, 3, 4]<\/li>\n\n\n\n<li>l = 1<\/li>\n\n\n\n<li>r = 4<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [4, 3, 2, 1]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing the entire array results in a completely inverted sequence.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Subarray at the Beginning:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [10, 20, 30, 40, 50]<\/li>\n\n\n\n<li>l = 1<\/li>\n\n\n\n<li>r = 3<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [30, 20, 10, 40, 50]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing the first three elements transforms [10, 20, 30] to [30, 20, 10].<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Subarray at the End:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [100, 200, 300, 400]<\/li>\n\n\n\n<li>l = 3<\/li>\n\n\n\n<li>r = 4<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [100, 200, 400, 300]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing the last two elements swaps 300 and 400.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Array with All Identical Elements:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [7, 7, 7, 7, 7]<\/li>\n\n\n\n<li>l = 2<\/li>\n\n\n\n<li>r = 4<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Output:<\/strong> [7, 7, 7, 7, 7]<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing a subarray where all elements are identical results in no visible change.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Invalid Indices (Assuming Input Validation):<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [1, 2, 3]<\/li>\n\n\n\n<li>l = 4<\/li>\n\n\n\n<li>r = 5<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Behavior:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The function assumes valid input as per constraints.<\/li>\n\n\n\n<li>If implemented with input validation, it should handle or reject such cases gracefully, possibly by raising an error or ignoring the operation.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Since l = 4 and r = 5 exceed the array bounds (len(arr) = 3), the function should ideally validate inputs before proceeding.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Empty Subarray (When <\/strong><strong>l &gt; r<\/strong><strong>):<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\n<ul class=\"wp-block-list\">\n<li>arr = [1, 2, 3, 4, 5]<\/li>\n\n\n\n<li>l = 5<\/li>\n\n\n\n<li>r = 3<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Expected Behavior:<\/strong>\n<ul class=\"wp-block-list\">\n<li>According to constraints, l &lt;= r, so such input should be considered invalid.<\/li>\n\n\n\n<li>The function may either perform no operation or handle it based on implementation specifics.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Explanation: <\/strong>Reversing from a higher index to a lower one doesn&#8217;t make sense and should be handled appropriately.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"24-time-and-space-complexity\">Time and Space Complexity:<\/h3>\n\n\n\n<p><strong>Time Complexity:<\/strong> O(n)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explanation:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Loop Iterations:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The while loop runs as long as left &lt; right<\/li>\n\n\n\n<li>For a subarray of length n = r &#8211; l + 1, the number of iterations is approximately n\/2<\/li>\n\n\n\n<li>Each iteration performs a constant-time swap operation<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Overall:<\/strong> The time complexity scales linearly with the size of the subarray, O(n)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Space Complexity:<\/strong> O(1)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explanation:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>In-Place Operations<\/strong>: The function performs swaps directly within the original array, requiring no additional space proportional to input size<\/li>\n\n\n\n<li><strong>Variables Used:<\/strong> Only a fixed number of variables (left, right) are used, regardless of the input size<\/li>\n\n\n\n<li><strong>Overall:<\/strong> The space complexity remains constant, O(1)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the document, 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<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:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">ENROLL in our free dsa course<\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone! In this article, we&#8217;ll guide you through the Python program to reverse an array using recursion and while loop. So, let&#8217;s get started with the [Problem Link]. Examples of the Python Program to Reverse Array Using Recursion and While Loop: Points to Keep in Mind: Expected Time Complexity: O(n) Expected Auxiliary Space: O(1)<\/p>\n","protected":false},"author":1,"featured_media":64,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,11],"class_list":{"0":"post-61","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-array","10":"tag-recursion"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Python-Program-to-Reverse-Array-Using-Recursion-and-While-Loop.webp","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\/61","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=61"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":548,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions\/548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/64"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}