Adobe Interview Question
Developer Program EngineersCountry: India
public void getFirstAndLast(int[] nums){
int maxSoFar = nums[0];
int temp = 0, last = 0, first = 0;
int currentMax = nums[0];
for (int i = 1; i < nums.length; i++) {
if(currentMax + nums[i] < currentMax){
temp = i;
currentMax = nums[i];
} else{
currentMax = currentMax + nums[i];
}
if(currentMax > maxSoFar){
last = i;
first = temp;
maxSoFar = currentMax;
}
}
System.out.println("First: "+first +" Last: "+last);
}
Java impl
public class MaxContiguousSum {
static class ValueObject{
protected final int max_so_far,start,end;
public ValueObject(int max_so_far, int start, int end){
this.max_so_far=max_so_far;
this.start=start;
this.end=end;
}
public String toString(){
return "{max_so_far: "+max_so_far+", start: "+start+", end: "+end+"}";
}
}
public static ValueObject maxSubArraySum(int a[]){
int max_so_far=a[0], i, start_curr=0, end_curr=0;
int curr_max=a[0];
for(i=1; i < a.length; i++){
if(a[i] > curr_max+a[i]){
start_curr=i;
end_curr=i;
}
curr_max = Math.max(a[i], curr_max+a[i]);
max_so_far = Math.max(max_so_far,curr_max);
end_curr++;
}
return new ValueObject(max_so_far, start_curr, end_curr);
}
public static void main(String[] args) {
int array[]={1,2,3,-4,-2,-6,8,-1,-3};
System.out.println("Result: "+maxSubArraySum(array));
}
}
Output:
Result: {max_so_far: 8, start: 6, end: 9}
Fix:
public static ValueObject maxSubArraySum(int a[]){
int max_so_far=a[0], i, start_curr=0, end_curr=0;
int curr_max=a[0];
for(i=1; i < a.length; i++){
if(a[i] > curr_max+a[i]){
start_curr=i;
end_curr=i;
}
curr_max = Math.max(a[i], curr_max+a[i]);
max_so_far = Math.max(max_so_far,curr_max);
end_curr++;
}
return new ValueObject(max_so_far, start_curr, end_curr-1);
}
Output:
Result: {max_so_far: 8, start: 6, end: 8}
Fixed to cover use-case scenarios and base-cases. I think that's the really tricky part of this problem, more than the coding itself.
public class MaxContiguousSum {
static class ValueObject{
protected final int max_so_far,start,end;
public ValueObject(int max_so_far, int start, int end){
this.max_so_far=max_so_far;
this.start=start;
this.end=end;
}
public String toString(){
return "{max_so_far: "+max_so_far+", start: "+start+", end: "+end+"}";
}
}
public static ValueObject maxSubArraySum(int a[]){
if(null==a || a.length==0){
return null;
}
else if(a.length==1){
return new ValueObject(a[0], 0, 0);
}
int max_so_far=a[0], i, start_curr=0, end_curr=0, start_prev=0, end_prev=0;
int curr_max=a[0];
//5,-1,7
for(i=1; i < a.length; i++){
// contiguous monotonically-increasing sum
if(curr_max + a[i] >= curr_max){
curr_max += a[i];
end_curr=i;
}
// If monotonically decreasing and next element is greater, then skip to next element
// If curr_max decreased, then skip to next element
if(curr_max+a[i] < a[i] || curr_max + a[i] < curr_max){
start_curr=i;
end_curr=i;
curr_max=a[i];
}
// Current sum is greater than max-so-far, then save indices of max-so-far
if(curr_max > max_so_far){
start_prev=start_curr;
end_prev=end_curr;
max_so_far=curr_max;
}
}
return new ValueObject(max_so_far, start_prev, end_prev);
}
public static void main(String[] args) {
int array[]={1,2,3,-4,-2,-6,8,-1,-3};
System.out.println("Result: "+maxSubArraySum(array));
}
}
Output:
Result: {max_so_far: 6, start: 0, end: 2}
- skum July 09, 2015