08 April 2017

arrayMaxConsecutiveSum

My solution:
int arrayMaxConsecutiveSum(int[] inputArray, int k) 
{
    var max = 0;
    var rem = 0;
    for (var i = 0; i <= inputArray.Length - k; i++)
    {
        if (i == 0)
            rem = inputArray.Where((i1, i2) => i2 >= i && i2 < i + k).Sum();
        else
            rem = rem + inputArray[i + k - 1] - inputArray[i - 1];

        max = Math.Max(rem, max);
    }

    return max;
}

Previous Next