Largest Continuous Sum
The question
Given an array of integers (positive and negative) find the largest continuous sum.
My solution
These are the steps I followed:
– Get the first number and add it to a variable where you will store the largest sum so far(largest).
– Create another variable(currentSum) where you will store the value of the current sum so far and assign the same number to it.
– Move to the next number. Calculate the sum of n1 and n2, if it is positive then assign it to currentSum.
– Check if currentSum is larger that largest. If it is, update largest.
– If currentSum became negative then assign 0 to it.
You have to go through each number in the array once, so the complexity is O(N).