{"id":421,"date":"2025-06-29T15:47:02","date_gmt":"2025-06-29T10:17:02","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=421"},"modified":"2025-06-29T15:47:04","modified_gmt":"2025-06-29T10:17:04","slug":"rearrange-array-elements-by-sign","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/","title":{"rendered":"Rearrange Array Elements by Sign | Leetcode 2149"},"content":{"rendered":"\n<p>Are you struggling with the &#8220;Rearrange Array Elements by Sign&#8221; problem on LeetCode? Don\u2019t worry! In this blog, we\u2019ll break down the problem, show you two ways to solve it (Brute Force and Optimal), and explain everything in simple, easy-to-understand English. Let\u2019s dive in!<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/rearrange-array-elements-by-sign\/description\/\" 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<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-c5237b5f-30b8-4b46-8432-386f84ad7543\" 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\/rearrange-array-elements-by-sign\/#0-what-does-the-problem-say\" style=\"\">What Does the Problem Say?<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#1-example-1\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#2-example-2\" style=\"\">Example 2<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#3-brute-force-solution\" style=\"\">Brute Force Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#4-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#5-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#6-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#7-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#8-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#9-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#10-optimal-solution\" style=\"\">Optimal Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#11-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#12-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#13-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#14-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#15-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#16-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/rearrange-array-elements-by-sign\/#17-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-what-does-the-problem-say\">What Does the Problem Say?<\/h2>\n\n\n\n<p>You are given an array of integers. Your task is to rearrange the elements so that positive and negative numbers appear one after another, starting with a positive number. The number of positive and negative numbers will always be the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-example-1\">Example 1<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [3, 1, -2, -5, 2, -4]<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>[3, -2, 1, -5, 2, -4]<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first element is positive (<code>3<\/code>), the second is negative (<code>-2<\/code>), the third is positive (<code>1<\/code>), the fourth is negative (<code>-5<\/code>), and so on.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-example-2\">Example 2<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [-1, 1]<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>[1, -1]<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-brute-force-solution\">Brute Force Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s think about how we can solve this problem step by step, using the simplest method:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Separate the Numbers:<\/strong><br>First, we need to separate the positive numbers and the negative numbers into two different lists. This way, we know exactly where all the positives and all the negatives are.<\/li>\n\n\n\n<li><strong>Rearrange Alternately:<\/strong><br>Next, we want to place the numbers back into the original array in an alternating order: first a positive, then a negative, then a positive, and so on.<\/li>\n\n\n\n<li><strong>Why Does This Work?<\/strong><br>Since the number of positive and negative numbers is always the same, we won\u2019t run out of either type while rearranging. By first collecting all positives and negatives, and then placing them back one by one, we make sure the order is correct.<\/li>\n<\/ol>\n\n\n\n<p>This approach is straightforward and easy to understand, even though it uses extra space for the two lists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-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 rearrangeArray(self, nums: List[int]) -&gt; List[int]:\n        pos = []\n        neg = []\n        n = len(nums)\n        for i in range(len(nums)):\n            if nums[i] &gt; 0:\n                pos.append(nums[i])\n            else:\n                neg.append(nums[i])\n        for i in range(len(pos)):\n            nums[2 * i] = pos[i]\n            nums[2 * i + 1] = neg[i]\n        return nums\" 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\">rearrangeArray<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        pos = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        neg = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[i] &gt; <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                pos.append(nums[i])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                neg.append(nums[i])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(pos)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            nums[<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> * i] = pos[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            nums[<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> * i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">] = neg[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> nums<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1:<\/strong>\u00a0We create two empty lists:\u00a0<code>pos<\/code>\u00a0for positive numbers and\u00a0<code>neg<\/code>\u00a0for negative numbers.<\/li>\n\n\n\n<li><strong>Step 2:<\/strong>\u00a0We loop through the original array. If a number is positive, we add it to\u00a0<code>pos<\/code>. If it\u2019s negative, we add it to\u00a0<code>neg<\/code>.<\/li>\n\n\n\n<li><strong>Step 3:<\/strong>\u00a0Now, we fill the original array. For every index\u00a0<code>i<\/code>, we put\u00a0<code>pos[i]<\/code>\u00a0at position\u00a0<code>2*i<\/code>\u00a0(even index) and\u00a0<code>neg[i]<\/code>\u00a0at position\u00a0<code>2*i+1<\/code>\u00a0(odd index).<\/li>\n\n\n\n<li><strong>Step 4:<\/strong>\u00a0Finally, we return the modified array.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let\u2019s dry run this code with an example:<\/p>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [3, 1, -2, -5, 2, -4]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>After separating:\n<ul class=\"wp-block-list\">\n<li><code>pos = [3, -\u00a0<\/code>neg = [-2, -5, -4]`<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Now, fill the original array:\n<ul class=\"wp-block-list\">\n<li><code>nums = 3<\/code>\u00a0(first positive)<\/li>\n\n\n\n<li><code>nums = -2<\/code>\u00a0(first negative)<\/li>\n\n\n\n<li><code>nums = 1<\/code>\u00a0(second positive)<\/li>\n\n\n\n<li><code>nums = -5<\/code>\u00a0(second negative)<\/li>\n\n\n\n<li><code>nums = 2<\/code>\u00a0(third positive)<\/li>\n\n\n\n<li><code>nums = -4<\/code>\u00a0(third negative)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>[3, -2, 1, -5, 2, -4]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(N) + O(N) = O(2N), which is the same as O(N).\n<ul class=\"wp-block-list\">\n<li>One pass to separate the numbers, one pass to rearrange.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(N)\n<ul class=\"wp-block-list\">\n<li>We use two extra lists to store positive and negative numbers.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force solution is very easy to understand and works well for small to medium-sized arrays. However, it uses extra space because we store the positive and negative numbers separately.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-optimal-solution\">Optimal Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s see if we can do better and use less extra space. Here\u2019s the idea:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Direct Placement:<\/strong><br>Instead of storing positive and negative numbers in two separate lists, we can use a new array to place numbers directly in their correct positions as we go through the original array.<\/li>\n\n\n\n<li><strong>Use Two Pointers:<\/strong><br>We use two pointers (or indices): one for the next positive position (starting at index 0), and one for the next negative position (starting at index 1).<\/li>\n\n\n\n<li><strong>Fill as We Go:<\/strong><br>As we loop through the original array:\n<ul class=\"wp-block-list\">\n<li>If we find a positive number, we put it at the next available even index (0, 2, 4, &#8230;).<\/li>\n\n\n\n<li>If we find a negative number, we put it at the next available odd index (1, 3, 5, &#8230;).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why Does This Work?<\/strong><br>This works because the number of positive and negative numbers is the same, so we never run out of space for either type.<\/li>\n<\/ol>\n\n\n\n<p>This approach is more space-efficient and only needs one extra array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-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 rearrangeArray(self, nums: List[int]) -&gt; List[int]:\n        n = len(nums)\n        ans = [0] * n\n        posIndex, negIndex = 0, 1\n        for i in range(n):\n            if nums[i] &lt; 0:\n                ans[negIndex] = nums[i]\n                negIndex += 2\n            else:\n                ans[posIndex] = nums[i]\n                posIndex += 2\n        return ans\" 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\">rearrangeArray<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        ans = [<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">] * n<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        posIndex, negIndex = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[i] &lt; <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                ans[negIndex] = nums[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                negIndex += <\/span><span style=\"color: #B5CEA8\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                ans[posIndex] = nums[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                posIndex += <\/span><span style=\"color: #B5CEA8\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> ans<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1:<\/strong>\u00a0We create a new array\u00a0<code>ans<\/code>\u00a0of the same size as\u00a0<code>nums<\/code>, filled with zeros.<\/li>\n\n\n\n<li><strong>Step 2:<\/strong>\u00a0We set\u00a0<code>posIndex<\/code>\u00a0to 0 (for the next positive number) and\u00a0<code>negIndex<\/code>\u00a0to 1 (for the next negative number).<\/li>\n\n\n\n<li><strong>Step 3:<\/strong>\u00a0We loop through the original array:\n<ul class=\"wp-block-list\">\n<li>If the current number is positive, we place it at\u00a0<code>ans[posIndex]<\/code>\u00a0and move\u00a0<code>posIndex<\/code>\u00a0by 2.<\/li>\n\n\n\n<li>If the current number is negative, we place it at\u00a0<code>ans[negIndex]<\/code>\u00a0and move\u00a0<code>negIndex<\/code>\u00a0by 2.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Step 4:<\/strong>\u00a0Finally, we return the filled\u00a0<code>ans<\/code>\u00a0array.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let\u2019s dry run this code with the same example:<\/p>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [3, 1, -2, -5, 2, -4]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initial\u00a0<code>ans = [0, 0,osIndex = 0<\/code>,\u00a0<code>negIndex = 1<\/code><\/li>\n<\/ul>\n\n\n\n<p>Loop through&nbsp;<code>nums<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>3<\/code>\u00a0is positive \u2192\u00a0<code>ans = 3<\/code>,\u00a0<code>posIndex = 2<\/code><\/li>\n\n\n\n<li><code>1<\/code>\u00a0is positive \u2192\u00a0<code>ans = 1<\/code>,\u00a0<code>posIndex = 4<\/code><\/li>\n\n\n\n<li><code>-2<\/code>\u00a0is negative \u2192\u00a0<code>ans = -2<\/code>,\u00a0<code>negIndex = 3<\/code><\/li>\n\n\n\n<li><code>-5<\/code>\u00a0is negative \u2192\u00a0<code>ans = -5<\/code>,\u00a0<code>negIndex = 5<\/code><\/li>\n\n\n\n<li><code>2<\/code>\u00a0is positive \u2192\u00a0<code>ans = 2<\/code>,\u00a0<code>posIndex = 6<\/code><\/li>\n\n\n\n<li><code>-4<\/code>\u00a0is negative \u2192\u00a0<code>ans = -4<\/code>,\u00a0<code>negIndex = 7<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>[3, -2, 1, -5, 2, -4]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(N)\n<ul class=\"wp-block-list\">\n<li>Only one pass through the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(N)\n<ul class=\"wp-block-list\">\n<li>Only one extra array is used for the result.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is more efficient and elegant. It avoids using two separate lists and fills the answer array in a single pass. This is the best approach for interviews and large datasets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Rearrange Array Elements by Sign&#8221; problem is a great way to practice array manipulation and pointer techniques. Start with the brute force method to build your understanding, and then move to the optimal solution for better performance and efficiency.<\/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","protected":false},"excerpt":{"rendered":"<p>Are you struggling with the &#8220;Rearrange Array Elements by Sign&#8221; problem on LeetCode? Don\u2019t worry! In this blog, we\u2019ll break down the problem, show you two ways to solve it (Brute Force and Optimal), and explain everything in simple, easy-to-understand English. Let\u2019s dive in! Here&#8217;s the [Problem Link] to begin with. What Does the Problem<\/p>\n","protected":false},"author":1,"featured_media":425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[7,19],"class_list":{"0":"post-421","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-array","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/rearrange-array-elements-by-sign-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\/421","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=421"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/421\/revisions\/424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/425"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}