The Maximum Sum Subarray ProblemIn this problem, we will be given an array of integers, and we need to find the subarray with the maximum sum of all the possible subarrays of the given array. Let us see an example to understand the problem. Input: array = [-2, -3, 4, -1, -2, 1, 5, -3] Output: 7 This array has the maximum sum of 7 all the subarrays. Brute Force ApproachThe simplest way to solve this problem is to calculate the sum of all the possible subarrays of the given array, compare them, and find the maximum possible sum. To solve this problem using this approach, we will need 3 loops. The first or the outermost loop will mark the beginning of each subarray. This loop will iterate over all the elements of the array. The second loop will mark the ending of the subarray. For each subarray, the ending index will equal n - 1, where n is the number of elements in the given array. The last loop will calculate the sum of each possible subarray between the starting and ending indexes marked by the outer two loops. While calculating the sum, we will continuously check for the maximum sum, and thus, in the end, we will get the maximum possible subarray sum. These are the steps we will follow to solve this problem using the brute force approach:
Code Output: The maximum sum of a subarray is 7 Time Complexity: The time complexity of this approach will be cubic since we are using three for loops. Hence, the final time complexity is O(n^3), where n is the number of elements given in the array. Space Complexity: We have not used any extra space in this approach; hence, the space complexity is constant, i.e., O(1). Optimal ApproachThe cubic time complexity is the major drawback of the brute force approach. However, we can optimize this approach slightly to decrease the time complexity from cubic to quadratic. As we can observe inside the third loop, we are calculating the sum of the following subarrays: array[s : s+1], array[s : s+2], array[s : s+3], and so on till array[s : e]. Hence, we are recalculating the sum for the previous subarray and then adding the current element. As sum(array[s : s+3] = sum(array[s : s+1]) + array[s+2]. These are the steps that we will follow:
Code Output: The maximum sum of a subarray is 7 Kadane's AlgorithmTo solve this problem, we will use the very famous Kadane's Algorithm. In this algorithm, we will maintain two variables. The first variable will store the value of the maximum sum of the continuous subarray that will end at any particular index. We will call this variable max_end_here. The second variable will store the maximum sum of all the individual subarrays we have encountered so far. We will call this one as max_till_now. At every iteration, we will check if the value of max_end_here exceeds the value of max_till_now. If yes, then we will update the value of max_till_now. In this way, at the end of the loop, we will get the maximum sum that a subarray can have in the array. To summarize the Kadane's algorithm in two sentences: -
Neglecting the subarray with a negative sum is because, for example, the array is [2, 3, -6, 10, 1]. The subarray [2, 3, -6] has a negative sum -1. Now, the subarray [10, 1] has the sum of 11. This is clear now: there is no point in adding the subarray [2, 3, -6] to the subarray [10, 1] as it will only decrease the sum. So this is our pseudocode: 1. Initializing Variables: max_till_now = -float("inf") max_end_here = 0 2. Traversing Over Each Item of the Array (a) max_end_here = max_end_here + array[i] (b) if (max_till_now < max_end_here) max_till_now = max_end_here (c) if (max_end_here < 0) max_end_here = 0 return max_till_now Let us understand the functioning using an example. We will dry run the code on an array, array = [-2, -4, 5, -1, -3, 1, 5, -2] 3. Initializing the Two Main Variables max_till_now = -float("inf") max_end_here = 0 4. Starting the Loop for i = 0, array[0] = -2 max_end_here = max_end_here + (-2) Setting the value of max_end_here = 0 since max_end_here < 0 and setting max_till_now = -2 for i=1, array[1] = -4 max_end_here = max_end_here + (-4) Since max_end_here < 0 and max_till_now = -2, max_till_now will become -6 Setting the value of max_end_here = 0 since max_end_here < 0 for i = 2, array[2] = 4 max_end_here = max_end_here + (5) max_end_here = 5 max_till_now is now set to 5 since the value of max_end_here is greater than max_till_now max_till_now = 5 for i = 3, array[3] = -1 max_end_here = max_end_here + (-1) max_end_here = 4 Not changing the value of max_till_now as it is greater than max_end_here for i = 4, array[4] = -3 max_end_here = max_end_here + (-3) max_end_here = 1 for i = 5, array[5] = 1 max_end_here = max_end_here + (1) max_ending_here = 2 for i = 6, array[6] = 5 max_end_here = max_end_here + (5) max_end_here = 7 max_till_now will now be set to 7 as the value of max_end_here is greater than max_till_now max_till_now = 7 for i = 7, array[7] = -2 max_end_here = max_end_here + (-2) max_end_here = 6 In the end, the value of max_till_now is our answer. Hence, the maximum sum of a subarray of the initial array is 7. These are the steps which we need to follow:
Below is the Python code for the above algorithm. Code Output: The maximum contiguous sum is 7 Time Complexity: We are using a linear loop to traverse over the elements of the array; hence, the time complexity is O(N) Auxiliary Space: We have not used any extra memory except to store the variables; hence, the space complexity is constant, i.e., O(1). Recursive Function for Kadane's AlgorithmThe basic strategy of Kadane's algorithm is to use "Divide-and-Conquer". We will break the array into smaller subarrays when we implement this function using the recursive function. Then, we will recursively call the function to find the maximum sum for the subarray of each subarray. The last step is to combine the individual solutions of the subproblems and find the final answer. Thus finding the maximum sum of the subarray of any given array. Now, we will see the algorithm to find the maximum sum of a subarray of any given integer array using the recursive function.
Below is the Python program for the implementation of the above recursive algorithm: Code Output: The maximum sum of a subarray is 7 Next TopicBilateral Filtering Using Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India