Find the Largest Sum of Consecutive Integers in an Array

This problem asks you to find the largest sum of consecutive integers in an array. So, for the example input above, the largest sum would be 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45.

Problem

Given an array of integers, write a function that returns the largest sum of consecutive integers in the array.
Example input: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Example output: 45 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)

Solution

This solution is in JavaScript. Our A.I. can create solutions in multiple languages.
by kayakane
// Solution:
// The optimal solution is to use a for loop to iterate through the array and add each element to a variable that keeps track of the sum.
// This solution is optimal because it is O(n) time complexity.

function largestSum(arr) {
  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
  return sum;
}

console.log(largestSum([1, 2, 3, 4, 5, 6, 7, 8, 9]));

A.I. Evaluation of the Solution

This solution is correct and demonstrates a level of completeness. The approach is straightforward and easy to understand.

Evaluated at: 2022-11-24 04:16:21