Find the largest sum of any contiguous subarray - MATLAB Cody - MATLAB Central

Problem 60201. Find the largest sum of any contiguous subarray

Difficulty:Rate
Given an array of N integers, find the contiguous subarray (which must contain at least one number) with the maximum sum and return its sum.
Here are some examples:
  1. max_subarray([1,2,3,-2,5]) should return 9 because the sum of the subarray [1,2,3,-2,5] is 9, and the sum of any other subarray is always less.
  2. max_subarray([-1,-2,-3,-4]) should return -1 because the maximum sum subarray is [-1].
  3. max_subarray([5,-1,6,-2,7]) should return 15 because the maximum sum subarray is [5, -1, 6, -2, 7].
  4. max_subarray([-2, 1, -3, 4, -1, 2, 1, -5, 4]) should return 6 because the maximum sum subarray is [4, -1, 2, 1].
  5. max_subarray([-2, -5, 6, -2, -3, 1, 5, -6]) should return 7 because the maximum sum subarray is [6, -2, -3, 1, 5].

Solution Stats

59.26% Correct | 40.74% Incorrect
Last Solution submitted on Feb 27, 2025

Solution Comments

Show comments
Join Cody Contest 2025 — Have Fun and Win Prizes!
...
We’re excited to invite you to Cody Contest 2025! 🎉 Pick a team,...
🚀 MATLAB EXPO 2025 – Day 2 Starts Tomorrow, November 13!
Don’t miss out on two incredible keynotes that will shape the future...
0

Problem Recent Solvers15

Suggested Problems

More from this Author53

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!