Amazon Interview Question
Developer Program EngineersSo if the m is given instead of a constant number 3, we just remember the previous sum instead of summing up in every loop.
int maxSum = 0, currentSum = 0, size = a.length;
for (int i = 0; i <= (size - m); i++) {
if (i < m - 1) {
currentSum += a[i];
}
else if (i = m - 1) {
maxSum = currentSum;
}
else if (i > m - 1) {
currentSum = currentSum + a[i] - a[i - m];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
}
return maxSum;
above code needs a bit correction :
int maxSum = 0, currentSum = 0, size = a.length;
for (int i = 0; i <= size-1; i++) {
if (i <= m - 1) {
currentSum += a[i];
}
if (i == m - 1) {
maxSum = currentSum;
}
if (i > m - 1) {
currentSum = currentSum + a[i] - a[i - m];
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
}
System.out.println(maxSum);
<pre lang="java" line="1" title="CodeMonkey21769" class="run-this">/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package subseq;
public class Main {
public static void main(String[] args) {
int a[] = {1,2,3,4,5,6,7,8};
int bigSum = 0,sum=0,index =0;
for(int i=0;i<a.length-2;i++)
{
int j = i;
sum=a[j]+a[j++]+a[j++];
if(sum>bigSum)
{
bigSum = sum;
index = i;
}
else
{
}
}
System.out.println("sum : "+bigSum+ " index: "+index);
}
}
</pre><pre title="CodeMonkey21769" input="yes">
</pre>
public static int getMax(int[] array, int m) {
int maxsum=0,oldsum,currsum=0;
for(int i=0;i<m;i++) maxsum+=array[i];
oldsum=maxsum;
for(int i=1;i<=(array.length-m);i++) {
currsum = oldsum-array[i-1]+array[i+m-1];
if(currsum>maxsum) {
maxsum = currsum;
}
oldsum = currsum;
}
return maxsum;
}
The question does not ask for the max sum.. it asks for the tuple that makes the max sum !!
package testPrograms;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class MaxSumTuple
{
private int[] tuple;
public MaxSumTuple()
{
tuple = new int[3];
}
public int[] findTuple(List<Integer> listInt)
{
int sumOld = 0,sumNew = 0;
if (listInt.size() <= 3)
{
for (int i = 0; i < listInt.size(); i++)
{
this.tuple[i] = listInt.get(i);
return tuple.clone();
}
}
for(int i=0;i<listInt.size() - 2;i++)
{
sumNew = listInt.get(i) + listInt.get(i+1) + listInt.get(i+2);
if(sumNew > sumOld)
{
sumOld = sumNew;
updateTuple(i,listInt);
}
}
return tuple.clone();
}
public void updateTuple(int index,List<Integer> list)
{
for(int i=0;i<tuple.length;i++)
{
tuple[i] = list.get(index + i);
}
}
public static void main(String []args)
{
List<Integer> list = new ArrayList<Integer>();
Random random = new Random();
for(int i=0;i<10;i++)
{
list.add(random.nextInt(100));
}
MaxSumTuple maxSumTuple = new MaxSumTuple();
int []tuple = maxSumTuple.findTuple(list);
System.out.println(list);
System.out.println(Arrays.toString(tuple));
}
}
if some variable m is given instead of 2, then complexity will become O(n2) from O(n).
Here it is:
package com.practice;
public class TupleWithMaxSum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] intArray={1,2,16,4,5,6,7};
int m=3;
int max=intArray[0];
for(int i=1;i<m;i++)
{
max= max + intArray[i];
}
int sum=0;
for(int i=1; i<intArray.length-m;i++)
{
for(int j=i;j<i+m;j++)
sum=sum+intArray[j];
if(sum>max)
max=sum;
sum=0;
}
System.out.println("Max sum: "+ max);
}
}
@Oshin,
- Anonymous August 07, 2010The question what I posted is exactly what he asked me.
For Example: If we have { 5 6 7 3 4 5 6 3} list of 8 int numbers we have and
sum of first 3 adjacent numbers (5 +6 +7) =18
sum of next 3 adjacent numbers (6+7+3) =16
sum of next (7+3+4) = 14
like wise we need to find out maximum sum of all these ......Please let me know if you are still not clear......