Largest subarray with 0 sum
PROBLEM :
Given an array having both positive an negative integers . Your task is to complete the function maxLen which returns the length of maximum subarray with 0 sum . The function takes two arguments an array A and n where n is the size of the array A .
Input:
The first line of input contains an element T denoting the No of test cases. Then T test cases follow. Each test case consist of 2 lines. The first line of each test case contains a number denoting the size of the array A. Then in the next line are space separated values of the array A .
Output:
For each test case output will be the length of the largest subarray which has sum 0 .
Constraints:
1<=T<=100
1<=N<=100
-1000<=A[]<=1000
Example:
Input
1
8
15 -2 2 -8 1 7 10 23
Output
5
Explanation
In the above test case the largest subarray with sum 0 will be -2 2 -8 1 7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*You are required to complete this function*/
int maxOf(int a,int b)
{
return a>b?a:b ;
}
int maxLen(int arr[],int no)
{
map<int,int> presum ;
int sum=0 ;
int maxlen=0 ;
for(int i=0;i<no;i++)
{
sum+=arr[i] ;
if(arr[i]==0&&maxlen==0)
maxlen=1 ;
if(sum==0)
maxlen=i+1 ;
if(presum.find(sum)!=presum.end())
maxlen=maxOf(maxlen,i-presum[sum]) ;
else
presum[sum]=i ;
}
return maxlen ;
}
---------------------------------------------------------------------------------
Given an array having both positive an negative integers . Your task is to complete the function maxLen which returns the length of maximum subarray with 0 sum . The function takes two arguments an array A and n where n is the size of the array A .
Input:
The first line of input contains an element T denoting the No of test cases. Then T test cases follow. Each test case consist of 2 lines. The first line of each test case contains a number denoting the size of the array A. Then in the next line are space separated values of the array A .
Output:
For each test case output will be the length of the largest subarray which has sum 0 .
Constraints:
1<=T<=100
1<=N<=100
-1000<=A[]<=1000
Example:
Input
1
8
15 -2 2 -8 1 7 10 23
Output
5
Explanation
In the above test case the largest subarray with sum 0 will be -2 2 -8 1 7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*You are required to complete this function*/
int maxOf(int a,int b)
{
return a>b?a:b ;
}
int maxLen(int arr[],int no)
{
map<int,int> presum ;
int sum=0 ;
int maxlen=0 ;
for(int i=0;i<no;i++)
{
sum+=arr[i] ;
if(arr[i]==0&&maxlen==0)
maxlen=1 ;
if(sum==0)
maxlen=i+1 ;
if(presum.find(sum)!=presum.end())
maxlen=maxOf(maxlen,i-presum[sum]) ;
else
presum[sum]=i ;
}
return maxlen ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment