Few months back, i went to gave GoldMann Sachhs interview and i was puzzled with this interview question:
They told me you have give an array of positive and negative integers and you have to find the sub array from the given array which sums up to the maximum.
For example:- if you have
{-2, -3, 4, -1, -2, 1, 5, -3}
If you see the above series you have a set of negative and positive numbers and now you have to find the first sub array which has the maximum sum, it can contain either one element or a set of elements.
Here in the above example if we have to find the sub array which has the highest sum. Now to approach for such question we should have a pseudo code first before deep diving into the programming code using java.
The pseudo-code for the above program is:-
They told me you have give an array of positive and negative integers and you have to find the sub array from the given array which sums up to the maximum.
For example:- if you have
{-2, -3, 4, -1, -2, 1, 5, -3}
If you see the above series you have a set of negative and positive numbers and now you have to find the first sub array which has the maximum sum, it can contain either one element or a set of elements.
Here in the above example if we have to find the sub array which has the highest sum. Now to approach for such question we should have a pseudo code first before deep diving into the programming code using java.
The pseudo-code for the above program is:-
Initialize:
max_so_far = 0
max_ending_here = 0
Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_ending_here < 0)
max_ending_here = 0
(c) if(max_so_far < max_ending_here)
max_so_far = max_ending_here
return max_so_far
Explanation:-
Simple idea of the above pseudo code algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far.
Lets take an example for a trial run of the above example:-
{-2, -3, 4, -1, -2, 1, 5, -3}
max_so_far = max_ending_here = 0for i=0, a[0] = -2 max_ending_here = max_ending_here + (-2) Set max_ending_here = 0 because max_ending_here < 0for i=1, a[1] = -3 max_ending_here = max_ending_here + (-3) Set max_ending_here = 0 because max_ending_here < 0for i=2, a[2] = 4 max_ending_here = max_ending_here + (4) max_ending_here = 4 max_so_far is updated to 4 because max_ending_here greater than max_so_far which was 0 till nowfor i=3, a[3] = -1 max_ending_here = max_ending_here + (-1) max_ending_here = 3for i=4, a[4] = -2 max_ending_here = max_ending_here + (-2) max_ending_here = 1for i=5, a[5] = 1 max_ending_here = max_ending_here + (1) max_ending_here = 2for i=6, a[6] = 5 max_ending_here = max_ending_here + (5) max_ending_here = 7 max_so_far is updated to 7 because max_ending_here is greater than max_so_farfor i=7, a[7] = -3 max_ending_here = max_ending_here + (-3) max_ending_here = 4
The java code for the above is:-
package com.javaLearningHub;public class MaxSubArray {public static void main(String[] args) {int a[] = { 3 ,-4 ,2 ,-7 ,8 ,-3 ,11 ,0 ,1 ,5 ,-10 };int size = a.length;int mx_sum = maxSubArraySum(a, size);System.out.println("The maximum sum is: " + mx_sum);}public static int maxSubArraySum(int a[], int size) {int max_so_far = 0, max_ending_here = 0;int i;for (i = 0; i < size; i++) {max_ending_here = max_ending_here + a[i];if (max_ending_here < 0) {max_ending_here = 0;} else if (max_so_far < max_ending_here) {max_so_far = max_ending_here;}}return max_so_far;}}This algorithm is also known as Kadane's algorithm. For more references please visit:-
http://en.wikipedia.org/wiki/Kadane%27s_Algorithm
Feel free to ask any questions. Post comments if u have any queries.
No comments:
Post a Comment