So, at every new element in the array we make two choices A) either add the new element to the current running sum OR B) start a new current sum altogether. Now, starting a new current sum is equivalent to starting a new sub-array.
The solutions that seem the simplest are usually the hardest to grasp: because mainly they are abstracted over several complex concepts.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is
O(N)
as since we would be iterating the array once. -
Memory
The memory usage is
O(1)
— A