Find the Largest Sum in an Array of Integers

This technical problem deals with finding the largest sum of contiguous integers in an array. An example input is given, along with the expected output.

Problem

Given an array of integers, find the largest sum of contiguous integers in the array.
Example input: [2, 3, -8, 7, -1, 2, 4]
Example output: 13

Solution

This solution is in C++. Our A.I. can create solutions in multiple languages.
by kayakane
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> input = {2, 3, -8, 7, -1, 2, 4};
    int max_sum = 0;
    int current_sum = 0;
    for (int i = 0; i < input.size(); i++) {
        current_sum += input[i];
        if (current_sum > max_sum) {
            max_sum = current_sum;
        }
        if (current_sum < 0) {
            current_sum = 0;
        }
    }
    cout << max_sum << endl;
    return 0;
}

/*

The solution is optimal because it only iterates through the array once.

*/

A.I. Evaluation of the Solution

The candidate's solution is optimal because it only iterates through the array once. The candidate's approach is also clear and easy to follow.

Evaluated at: 2022-11-21 18:15:24