Maximum Sub Array Sum 2 Dimensional
In this class, We discuss Maximum Sub Array Sum 2 Dimensional.
The reader should have prior knowledge of kadane’s algorithm. Click Here.
For Complete YouTube Video: Click Here
The below diagram shows the two-dimensional array.
The orange box is the sub-array with the maximum sum.
The sum of all the elements in the sub-array should be maximum.
We can take a single element as sub array.
We can take all the elements as sub array.
Adding all the elements in the orange box, we get the value 29.
In kadane’s algorithm, we identified the maximum sub-array sum in a single-dimensional array.
The same algorithm finds the sub-array sum in two dimensional.
Procedure:
Take the first line in the array and apply kadane’s algorithm.
The first line will return the maximum sum of 3.
Take the first two lines, add the elements in each column, and make it a single-dimensional array.
Again apply kadane’s algorithm on the newly formed single-dimensional array.
Similarly, Take all the combinations of lines and add the elements and apply kadane’s algorithm.
The combination which gives the maximum sum is the required output.
The complete explanation is in the video link provided above.
Code: