Maximum Sum of Contiguous Subarray

Given an array of n integers, the maximum sum of any contiguous subarray of the array can be found by using the example input and output above.

Problem

Given an array of n integers, find the maximum sum of any contiguous subarray of the array.
Example input: [34, -50, 42, 14, -5, 86]
Example output: 137

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 maxSubArray(vector<int>& nums) {
    int max_sum = nums[0];
    int curr_sum = nums[0];
    for (int i = 1; i < nums.size(); i++) {
        curr_sum = max(nums[i], curr_sum + nums[i]);
        max_sum = max(max_sum, curr_sum);
    }
    return max_sum;
}

int main() {
    vector<int> nums = {34, -50, 42, 14, -5, 86};
    cout << maxSubArray(nums) << endl;
    return 0;
}

A.I. Evaluation of the Solution

The candidate's solution correctly finds the maximum sum of any contiguous subarray of the array. The candidate's approach is to keep track of the current sum and update the maximum sum as needed. This is a good approach.

Evaluated at: 2022-12-01 10:15:31