Array Journey - Maximum sum rate
Description:
A pointer is at the first index of any array/Array list, and a sum start at the value of position. Move the pointer to the end of the array in the ranging from 1 to max step. At each step add the value of an array element at the pointer to the sum. Return the max sum that is achieved.
The utility code snippet for the above problem.
private static int calculateMaxSumRateWithMaxSteps(List<Integer> arrList, int maxSteps){
Integer [] arr = arrList.toArray(new Integer[arrList.size()]);
if(arr.length == 0){
return arr[0];
}
if(arr.length == 1){
return arr[1];
}
int firstSum = 0;
int secondSum = 0;
int outputSum = 0;
int count = 0;
for(int i = 0; i < arr.length; i++){
if(i == 0){
firstSum = arr[i]+arr[i+1];
secondSum = arr[i]+arr[i+1+maxSteps-1];
count +=maxSteps;
}else{
if(count <= arr.length){
firstSum = outputSum + arr[i+1];
secondSum = outputSum + arr[i+1+maxSteps-1];
count += maxSteps;
}
}
if(firstSum>secondSum){
outputSum = firstSum;
}else{
outputSum =secondSum;
}
}
return outputSum;
}
}

This comment has been removed by the author.
ReplyDeleteIt doesn't work. It just for work for case described on picture above, but all other tests case would fail.
ReplyDelete