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

Commit

Permalink
r program to find maximum sum
Browse files Browse the repository at this point in the history
  • Loading branch information
ApurvaR1 authored and ApurvaR1 committed Nov 30, 2023
1 parent 81e8dfc commit 8fada65
Showing 1 changed file with 16 additions and 0 deletions.
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)

0 comments on commit 8fada65

Please sign in to comment.