  # If an array contains positive and negative integers then how can we find largest subarray with sum as zero?

250 views
If an array contains positive and negative integers then how can we find largest subarray with sum as zero? posted Nov 29, 2013
It's a standard bruteforce or backtrack problem other way is to use dynamic programming.

+1 vote

Try this method to get your result:

``````   public static Boolean printZeroSumSubarray(int arr[]) // return true when sub array sum is zero
{
// Creates an empty hashMap hM
HashMap<Integer, Integer> hM = new HashMap<Integer, Integer>();

// Initialize sum of elements
int sum = 0;

// Traverse through the given array
for (int i = 0; i < arr.length; i++)
{
// Add current element to sum
sum += arr[i];

// Return true in following cases
// a) Current element is 0
// b) sum of elements from 0 to i is 0
// c) sum is already present in hash map
if (arr[i] == 0 || sum == 0 || hM.get(sum) != null)
return true;

// Add sum to hash map
hM.put(sum, i);
}

// We reach here only when there is no subarray with 0 sum
return false;
}
``````

from this method you will get the different subarray and get thier length then print the subarray of maximum length. answer Mar 22, 2016
Similar Questions