Skip to content

Commit 6b8783d

Browse files
committed
Max subarray sum
1 parent a4072ed commit 6b8783d

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

codes/max_sum_sub_array.mojo

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Given an integer array nums, find the subarray with the largest sum, and return its sum.
2+
3+
4+
# Function to find the subarray with the maximum sum
5+
fn max_sum_sub_array(nums: List[Int]) -> Int:
6+
# If the list is empty, return 0 as no subarray exists
7+
if len(nums) == 0:
8+
return 0
9+
10+
# Initialize the running sum and max sum with the first element
11+
# running_sum: current subarray sum being tracked
12+
# max_sum: maximum subarray sum seen so far
13+
running_sum, max_sum = nums[0], nums[0]
14+
15+
# Iterate over the list starting from the second element
16+
for idx in range(1, len(nums)):
17+
# Decide whether to extend the previous subarray or start a new subarray at current index
18+
running_sum = max(running_sum + nums[idx], nums[idx])
19+
20+
# Update max_sum if the current running_sum is greater
21+
max_sum = max(max_sum, running_sum)
22+
23+
# Return the maximum subarray sum found
24+
return max_sum
25+
26+
27+
def main():
28+
nums = List(-2, 1, -3, 4, -1, 2, 1, -5, 4)
29+
max_sum = max_sum_sub_array(nums)
30+
debug_assert(max_sum == 6, "Assertion failed")
31+
nums = List(5,4,-1,7,8)
32+
max_sum = max_sum_sub_array(nums)
33+
debug_assert(max_sum == 23, "Assertion failed")

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
- [ Buy and Sell Stock 2](buy_and_sell_stock_2.md) ➔ Maximize profit by buying and selling multiple times.
77
- [ Longest Substring Without Repeating Characters](longest_substr_no_char_repeats.md) ➔ Given a string s, find the length of the longest substring without duplicate characters.
88
- [ Product of Array Except Self](product_of_array_except_self.md) ➔ Return an array where each element is the product of all others in the input array.
9+
- [ Maximum Subarray](max_sum_sub_array.md) ➔ Given an integer array nums, find the subarray with the largest sum, and return its sum.

docs/max_sum_sub_array.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Given an integer array nums, find the subarray with the largest sum, and return its sum.
2+
3+
### Kadane’s Algorithm: At each step, you decide whether to include the current number in the previous subarray (i.e., running_sum + nums[idx]) or start a new subarray from the current number (nums[idx]).
4+
5+
### It maintains the maximum sum seen so far and runs in O(n) time with O(1) space.
6+
7+
# Function to find the subarray with the maximum sum
8+
fn max_sum_sub_array(nums: List[Int]) -> Int:
9+
# If the list is empty, return 0 as no subarray exists
10+
if len(nums) == 0:
11+
return 0
12+
13+
# Initialize the running sum and max sum with the first element
14+
# running_sum: current subarray sum being tracked
15+
# max_sum: maximum subarray sum seen so far
16+
running_sum, max_sum = nums[0], nums[0]
17+
18+
# Iterate over the list starting from the second element
19+
for idx in range(1, len(nums)):
20+
# Decide whether to extend the previous subarray or start a new subarray at current index
21+
running_sum = max(running_sum + nums[idx], nums[idx])
22+
23+
# Update max_sum if the current running_sum is greater
24+
max_sum = max(max_sum, running_sum)
25+
26+
return max_sum
27+
28+
def main():
29+
nums = List(-2, 1, -3, 4, -1, 2, 1, -5, 4)
30+
max_sum = max_sum_sub_array(nums)
31+
debug_assert(max_sum == 6, "Assertion failed")
32+
nums = List(5,4,-1,7,8)
33+
max_sum = max_sum_sub_array(nums)
34+
debug_assert(max_sum == 23, "Assertion failed")

0 commit comments

Comments
 (0)