We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Search
- Maximum Subarray Sum
- Discussions
Maximum Subarray Sum
Maximum Subarray Sum
Sort by
recency
|
344 Discussions
|
Please Login in order to post a comment
My Java 8 solution, passing all test-cases, for Max Score of 60
The idea is to do a "continuous trailing prefix modulo sum" as we iterate through the array, while comparing that with the
maxModSum
that we have seen so far & updating that as required. We maintain aprefixSumTreeSet
to keep track of all the distinct prefixSums we have seen so far. We check if we have encountered a prefixSum that's higher than thecurPrefixSum
, and if so: then we use thehigherPrefixTS
value to updatemaxModSum
as required.By using this approach, we only need to loop through the array once from start to finish (costing
O(n)
time). In each iteration, we spend some time going through theprefixSumTreeSet
to locate the "higher" element, or to add a new element to the TreeSet (costingO(log(n))
time. This results in an overall Time Complexity ofO(n * log(n))
.Here is my Python solution!
No need to use tree or sorted set. Just store all the prefix sums and sort. This idea comes from a different problem of minimising loss on house purchasing.
pure java... package geeksforgeeks; import java.util.*;
public class Maxnumber {
}