{"id":518,"date":"2025-07-07T18:34:17","date_gmt":"2025-07-07T13:04:17","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=518"},"modified":"2025-07-07T18:34:19","modified_gmt":"2025-07-07T13:04:19","slug":"largest-subarray-with-0-sum","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/","title":{"rendered":"Largest Subarray with 0 Sum \u2013 GeeksforGeeks Solution Explained"},"content":{"rendered":"\n<p>Are you stuck on the &#8220;Largest subarray with 0 sum&#8221; problem from GeeksforGeeks? Don\u2019t worry! In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/largest-subarray-with-0-sum\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-f1b680ad-ce2a-43c1-85bb-c79f239c7dc1\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"false\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><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\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 \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#0-what-does-the-problem-say\" style=\"\">What Does the Problem Say?<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#1-example-1\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#2-example-2\" style=\"\">Example 2<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#3-brute-force-solution\" style=\"\">Brute Force Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#4-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#5-code-implementation\" style=\"\">Code Implementation<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#6-code-explanation\" style=\"\">Code Explanation<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#7-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#8-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#9-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#10-optimal-solution\" style=\"\">Optimal Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#11-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#12-code-implementation\" style=\"\">Code Implementation<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#13-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#14-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#15-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#16-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/largest-subarray-with-0-sum\/#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 find the length of the largest subarray (continuous part of the array) whose elements sum up to 0.<\/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>arr = [15, -2, 2, -8, 1, 7, 10, 23]<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>5<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong><br>The largest subarray with sum 0 is&nbsp;<code>[-2, 2, -8, 1, 7]<\/code>&nbsp;(from index 1 to 5).<\/p>\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>arr = [1]<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>0<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong><br>There is no subarray with sum 0.<\/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 solve the problem step by step in the simplest way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Try Every Subarray:<\/strong><br>For every possible starting index, check every possible ending index and calculate the sum of the subarray from start to end.<\/li>\n\n\n\n<li><strong>Check for Zero Sum:<\/strong><br>If the sum is zero, update the maximum length found so far.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We are checking every possible subarray, so we won\u2019t miss any that sum to zero.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large arrays.<\/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 padding-bottom-disabled cbp-has-line-numbers\" 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;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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 maxLen(self, n, arr):\n        max_length = 0\n        n = len(arr)\n        for i in range(n):\n            sum = 0\n            for j in range(i, n):\n                sum += arr[j]  # Add current element to sum\n                if sum == 0:\n                    max_length = max(max_length, j - i + 1)  # Update max_length if sum is zero\n        return max_length\" 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\">maxLen<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        max_length = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr)<\/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: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> j <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(i, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\"> += arr[j]  <\/span><span style=\"color: #6A9955\"># Add current element to sum<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\"> == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    max_length = <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(max_length, j - i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)  <\/span><span style=\"color: #6A9955\"># Update max_length if sum is zero<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> max_length<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-code-explanation\">Code Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use two loops: the outer loop picks the start index, and the inner loop picks the end index.<\/li>\n\n\n\n<li>For each subarray, we calculate the sum.<\/li>\n\n\n\n<li>If the sum is zero, we check if the length is the largest found so far.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr = [15, -2, 2, -8, 1, 7, 10, 23]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start at index 0:\n<ul class=\"wp-block-list\">\n<li>Sum from 0 to 0: 15 (not zero)<\/li>\n\n\n\n<li>Sum from 0 to 1: 13 (not zero)<\/li>\n\n\n\n<li>&#8230;<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Start at index 1:\n<ul class=\"wp-block-list\">\n<li>Sum from 1 to 1: -2 (not zero)<\/li>\n\n\n\n<li>Sum from 1 to 2: 0 \u2192 length = 2<\/li>\n\n\n\n<li>Sum from 1 to 5: 0 \u2192 length = 5 (update max_length)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Continue for all start indices.<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>max_length = 5<\/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^2)<br>Two nested loops for all subarrays.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only a few variables are used.<\/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 approach is very simple and good for understanding, but it\u2019s too slow for large arrays.<\/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>We can solve this problem much faster using a hash map (dictionary):<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use Prefix Sums:<\/strong><br>As we go through the array, keep track of the sum of elements from the start to the current index (prefix sum).<\/li>\n\n\n\n<li><strong>Store Sums in a Map:<\/strong><br>If the same prefix sum appears again, it means the elements between the two indices sum to 0.<\/li>\n\n\n\n<li><strong>Check for Zero Sum:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the prefix sum is zero at any index, the subarray from the start to that index sums to zero.<\/li>\n\n\n\n<li>If we have seen the prefix sum before, the subarray between the previous index and the current index sums to zero.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Why does this work?<\/strong><br>If the sum from index 0 to i is S, and the sum from 0 to j is also S, then the sum from i+1 to j must be zero.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"12-code-implementation\">Code Implementation<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" 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;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);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 maxLen(self, n, arr):\n        n = len(arr)\n        prefix_sum = {}  # Dictionary to store first occurrence of each prefix sum\n        maxi = 0  # To keep track of maximum length found\n        sum = 0  # To store the current prefix sum\n        for i in range(n):\n            sum += arr[i]  # Update prefix sum\n            if sum == 0:\n                maxi = i + 1  # If prefix sum is zero, update maxi\n            else:\n                if sum in prefix_sum:\n                    maxi = max(maxi, i - prefix_sum[sum])  # Update maxi if sum seen before\n                else:\n                    prefix_sum[sum] = i  # Store first occurrence of this sum\n        return maxi\" 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\">maxLen<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/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\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        prefix_sum = {}  <\/span><span style=\"color: #6A9955\"># Dictionary to store first occurrence of each prefix sum<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        maxi = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># To keep track of maximum length found<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># To store the current prefix sum<\/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: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\"> += arr[i]  <\/span><span style=\"color: #6A9955\"># Update prefix sum<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\"> == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                maxi = i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># If prefix sum is zero, update maxi<\/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\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> prefix_sum:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    maxi = <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(maxi, i - prefix_sum[<\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\">])  <\/span><span style=\"color: #6A9955\"># Update maxi if sum seen before<\/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\">                    prefix_sum[<\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\">] = i  <\/span><span style=\"color: #6A9955\"># Store first occurrence of this sum<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> maxi<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/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>We keep a running sum (<code>sum<\/code>) as we go through the array.<\/li>\n\n\n\n<li>If\u00a0<code>sum<\/code>\u00a0is zero, it means the subarray from the start to the current index sums to zero.<\/li>\n\n\n\n<li>We use a dictionary (<code>prefix_sum<\/code>) to store the first index where each sum occurs.<\/li>\n\n\n\n<li>If we see the same sum again, the subarray between the previous index and the current index sums to zero.<\/li>\n\n\n\n<li>We update the maximum length whenever we find a longer subarray.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr = [15, -2, 2, -8, 1, 7, 10, 23]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>i = 0: sum = 15, store 15:0<\/li>\n\n\n\n<li>i = 1: sum = 13, store 13:1<\/li>\n\n\n\n<li>i = 2: sum = 15, seen before at 0 \u2192 length = 2 (update maxi)<\/li>\n\n\n\n<li>i = 3: sum = 7, store 7:3<\/li>\n\n\n\n<li>i = 4: sum = 8, store 8:4<\/li>\n\n\n\n<li>i = 5: sum = 15, seen before at 0 \u2192 length = 5 (update maxi)<\/li>\n\n\n\n<li>Continue for all indices.<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>maxi = 5<\/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)<br>Only one pass through the array.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(N)<br>For storing prefix sums in the dictionary.<\/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 fast and efficient. It uses a hash map to keep track of prefix sums and finds the answer in a single pass.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Largest subarray with 0 sum&#8221; problem is a great example of how to improve your solution from brute force to optimal. Start with the simple approach to build understanding, then use prefix sums and hashing for the best performance.<\/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\/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\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you stuck on the &#8220;Largest subarray with 0 sum&#8221; problem from GeeksforGeeks? Don\u2019t worry! In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with comments, dry runs, and clear explanations. Here&#8217;s the [Problem Link] to begin with. What Does the Problem<\/p>\n","protected":false},"author":1,"featured_media":520,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[7,18],"class_list":{"0":"post-518","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-array","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/longest-subarray-with-sum-zero-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\/518","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=518"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":521,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions\/521"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/520"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}