Amazon Interview Question
SDE1sCountry: India
Interview Type: In-Person
#include<stdio.h>
#define max(x,y) (x>y?x:y)
int findsum(int a[],int n)
{
if(n==0)
return (a[0]>0?a[0]:0);
int i,x,y,z,tem;
x=a[0]>0?a[0]:0;
y=a[1]>0?a[1]:0;
z=0;
for(i=2;i<n;i++)
{
tem=z;
z=x;
x=y;
y=(max(tem,z))+(max(a[i],0));
}
return max(x,y);
}
int main()
{
int a[]={-5,-6,-10,-40,50,-35};
int n=sizeof(a)/sizeof(a[0]);
printf("max sum=%d",findsum(a,n));
return 0;
}
Dynamic programming with Memoization
static int MaxSumBase(int[] input)
{
Hashtable Sums = new Hashtable();
int [] memo = new int[input.Length];
for(int i=0; i< memo.Length;i++)
memo[i]= int.MinValue;
for (int i = 0; i < input.Length; i++)
Sums.Add(i, MaxSum(input, i, memo));
return Max(Sums);
}
static int MaxSum(int[] input, int index,int []memo)
{
if (index >= input.Length)
return 0;
if(memo[index] != int.MinValue)
return memo[index];
Hashtable Sums = new Hashtable();
for(int i= index+2; i< input.Length; i++)
Sums.Add(i,MaxSum(input,i,memo));
int maxofSums = Max(Sums);
memo[index] = input[index] + (maxofSums != int.MinValue ? maxofSums : 0);
return memo[index];
}
static int Max(Hashtable sums)
{
int max = int.MinValue;
foreach (int entry in sums.Values)
if (entry > max)
max = entry;
return max;
}
Would you please clarify the task: is the task to find the largest sum of consequent numbers?
Rather the opposite: the task is to find the largest sum using the given numbers, with the condition that no 2 numbers can be next to each other in the array. So if array is {2,4,6,8,10}, then the largest sum is sum of {2,6,10}, and that's fine because none of these 3 numbers are adjacent in the original array. If the array is {-2, -4, -6, -8, -10}, then the largest sum is simply {-2}.
I dont know whether this solution is appropriate or not...
if I traverse the array linearly twice, I can find the largest and second largest element respectively in O(n+n) = O(n) time.
Thus we are done with the maximum sum as the largest and second largest element gives the maximum sum.
Now if we introduce the condition while finding the second largest element I think, we are done!
Need comment from all of you!
How about
public int nonContinousMax(int[] array) {
int maxIncludedPrev = 0;
int maxExcludedPrev = 0;
for (int i = 0; i < array.length; i++) {
if (i == 0) {
maxIncludedPrev = array[i];
continue;
} else {
if ((maxExcludedPrev + array[i]) > maxExcludedPrev) {
maxExcludedPrev = maxExcludedPrev + array[i];
maxIncludedPrev = maxExcludedPrev + maxIncludedPrev;
maxExcludedPrev = maxIncludedPrev - maxExcludedPrev;
maxIncludedPrev = maxIncludedPrev - maxExcludedPrev;
} else {
maxExcludedPrev=Math.max(maxExcludedPrev, maxIncludedPrev);
maxIncludedPrev=maxExcludedPrev;
}
}
}
return Math.max(maxIncludedPrev, maxExcludedPrev);
}
Similar question has been solved using dynamic programming at: question?id=19378664
Follows the recursive structure:
currentSum = max{A[i], max{A[i-2] + A[i], A[i-2]}, max{A[i-3] + A[i], A[i-3]}, } // i runs from 2 to N.
Full working implementation in java is given below. Give the input numbers using space as delimiter.
Eg: 1 - 1 3 8 4
1 5 3 9 4
1 2 3 4 5
- Murali Mohan June 21, 2013