-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from narayancse/patch-4
Create Find the length of largest subarray with 0 sum
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |