Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

r program to find maximum sum #4808

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
findMaxSubarraySum <- function(arr) {
max_so_far <- arr[1]
max_ending_here <- arr[1]

for (i in 2:length(arr)) {
max_ending_here <- max(arr[i], max_ending_here + arr[i])
max_so_far <- max(max_so_far, max_ending_here)
}

return(max_so_far)
}

# Test the function with the provided input
input_array <- c(-2, -3, 4, -1, -2, 1, 5, -3)
max_sum <- findMaxSubarraySum(input_array)
print(max_sum)