#53
Medium Algorithms Maximum subarray
Array Divide and Conquer Dynamic Programming
53.0% acceptance
Jan 12, 2026
37567
1593
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
let mut max_sum = nums[0];
let mut current_sum = nums[0];
for i in 1..nums.len() {
current_sum = current_sum.max(0) + nums[i];
max_sum = max_sum.max(current_sum);
}
max_sum
}
}