jinshuxianty
BAN USER
Comments (2)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
TC O(N); SC O(1)
public int getMaxLen(int[] arr ){
if( arr == null  arr.length == 0 ){
return 0;
}
int max = 0, sum = 0, len = arr.length;
int sign = 1;
for( int i=0; i<len; i++ ){
sign = (arr[i] == 1)?1:1;
arr[i] = sign * (len+1);
sum += sign;
if( sum > 0 ){
sign = (arr[sum1]>=0)?1:1;
int pos1 = arr[sum1]*sign;
if( pos1 == len+1 ){
arr[sum1] = sign * i;
}else{
int tmp = i  pos1 ;
max = (tmp>max)?tmp:max;
}
}else if( sum == 0 ){
int tmp = i+1;
max = (tmp>max)?tmp:max;
}
}
sum = 0;
for( int i=0; i<len; i++ ){
sign = (arr[i] >= 0)?1:1;
arr[i] = len+1 ;
sum += sign;
if( sum < 0 ){
if( arr[sum1] == len + 1 ){
arr[sum1] = i;
}else{
int tmp = i  arr[sum1] ;
max = (tmp>max)?tmp:max;
}
}
}
return max;
}

jinshuxianty
June 28, 2013 Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Open Chat in New Window
Open Chat in New Window
This idea uses the space of the origin array.
 jinshuxianty June 28, 2013The idea is to store the sum of all elements in the specific pos of the array.
Whenever the sum is met again, check it with the first time the sum was met and update the max.