Skip to content

Commit

Permalink
Merge pull request #38 from narayancse/patch-4
Browse files Browse the repository at this point in the history
Create Find the length of largest subarray with 0 sum
  • Loading branch information
narayancseian authored Oct 21, 2021
2 parents 7f75ef8 + 184bbdf commit 7f17976
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Hashing/Find the length of largest subarray with 0 sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* A simple C++ program to find
largest subarray with 0 sum */
#include <bits/stdc++.h>
using namespace std;

// Returns length of the largest
// subarray with 0 sum
int maxLen(int arr[], int n)
{
// Initialize result
int max_len = 0;

// Pick a starting point
for (int i = 0; i < n; i++) {

// Initialize currr_sum for
// every starting point
int curr_sum = 0;

// try all subarrays starting with 'i'
for (int j = i; j < n; j++) {
curr_sum += arr[j];

// If curr_sum becomes 0,
// then update max_len
// if required
if (curr_sum == 0)
max_len = max(max_len, j - i + 1);
}
}
return max_len;
}

// Driver Code
int main()
{
int arr[] = { 15, -2, 2, -8, 1, 7, 10, 23 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Length of the longest 0 sum subarray is "
<< maxLen(arr, n);
return 0;
}

0 comments on commit 7f17976

Please sign in to comment.