


You are given an array/list ARR consisting of N integers. Your task is to find the maximum possible sum of a non-empty subarray(contagious) of this array.
Note: An array C is a subarray of array D if it can be obtained by deletion of several elements(possibly zero) from the beginning and the end of array D.
For e.g.- All the non-empty subarrays of array [1,2,3] are [1], [2], [3], [1,2], [2,3], [1,2,3].
The first line of input contains a single integer ‘N’ denoting the number of elements in the array/list.
The second line of input contains ‘N’ single space-separated integers, denoting the elements of the array.
Output Format :
Print the maximum possible sum of any subarray of the array/list.
Note: You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= N <= 5*10^5
-10^9 <= ARR[i] <=10^9
Time Limit: 1sec
4
2 -7 -11 13
13
The maximum sum subarray has index range [3, 3].
5
6 -1 3 -4 3
8
The maximum sum subarray has index range [0, 2].
Iterate over all possible subarrays and return the maximum possible sum
We will iterate through all possible boundaries of the subarrays in the given array with the help of two nested loops.
Then, we will iterate through each subarray with the help of another loop and find the sum of the subarray. We will maintain the maximum subarray sum through our iterations and finally return it.
O(N^3), where N denotes the number of elements in an array.
As there are N*(N+1)/2 subarrays in total, and we are iterating over every subarray in linear time, the overall time complexity will be O(N^3).
O(1), constant space is used.