#include <iostream>
#include <vector>
int maxSubarrayBruteForce(const std::vector<int>& nums) {
int maxSum = INT_MIN;
int n = nums.size();
for (int i = 0; i < n; i++) {
int currentSum = 0;
for (int j = i; j < n; j++) {
currentSum += nums[j];
maxSum = std::max(maxSum, currentSum);
}
}
return maxSum;
}
int main() {
std::vector<int> nums = { -2, 3, -1, 5, 4, -7, 2, 1, -3, 6 };
int max = maxSubarraySumBruteForce(nums);
std::cout << "Maximum Subarray Sum: " << max << std::endl;
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter