Comments on: Programming Interview Questions 3: Largest Continuous Sum /2011/09/24/programming-interview-questions-3-largest-continuous-sum/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-3-largest-continuous-sum Information Retrieval and Machine Learning Fri, 15 Aug 2014 10:58:00 +0000 hourly 1 http://wordpress.org/?v=3.8.4 By: vikas /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7745 Fri, 15 Aug 2014 10:58:00 +0000 /?p=516#comment-7745 1 + 50 + -20 + 1 + 20 = 52

]]>
By: avimas /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7729 Tue, 01 Jul 2014 07:36:00 +0000 /?p=516#comment-7729 doesn’t work for {1, 2, 3, 4, -6, 7, 7, 7}

see my correction:

public static void large_cont(int[] values){

int tstart = 0;

int startpos = 0;

int endpos = 0;

int cursum = values[0];

int maxsum = values[0];

if(values.length > 1){

for(int i = 1; i cursum + values[i] || values[i] > values[i]+ cursum ){

cursum = values[i];

tstart = i;

}

else{

cursum += values[i];

}

if(cursum > maxsum){

maxsum = cursum;

startpos = tstart;

endpos = i;

}

}

}

System.out.println(maxsum + ” ” + startpos + ” ” + endpos);

}

]]>
By: christan /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7667 Wed, 25 Dec 2013 17:32:00 +0000 /?p=516#comment-7667 for {-40,1,40,-50,1,50,-20,1,20,0,0} the algorithm should give 52, not 51. Start from the second 1:
1+50+(-20)+1+20 = 52

]]>
By: yoda /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7565 Sat, 23 Mar 2013 22:50:00 +0000 /?p=516#comment-7565 time complexity is O(N) and space complexity is O(1)

]]>
By: yoda /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7564 Sat, 23 Mar 2013 22:49:00 +0000 /?p=516#comment-7564 /*this java code prints maximum contiguous sub array and its sum*/

public class MaxSubArray {

public static void maxSubArray(int[] Q){

int A1=Integer.MIN_VALUE,A=0;

int s=0,s1=0;

int e=0,e1=0;

for (int i = 0; i Q[i]+A){

A=Q[i];

s=i;

e=i;

}

else{

A=Q[i]+A;

e=i;

}

if(A>A1){

A1=A;

s1=s;

e1=e;

}

}

System.out.println(“the maximum subarray sum is: “+A1);

for (int i = s1; i <= e1; i++) {

System.out.print(Q[i]+" ");

}

System.out.println();

}

public static void main(String[] args) {

int[] Q={13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7};

int[] Q1={1,2,3,4,5,6,7,8,9,10};

int[]Q2={-10,-9,-8,-7,-6,-5,-4,-3,-2,-1};

int[] Q3={-40,1,40,-50,1,50,-20,1,20,0,0};

maxSubArray(Q);

maxSubArray(Q1);

maxSubArray(Q2);

maxSubArray(Q3);

}

}

]]>
By: Rohit /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7543 Thu, 07 Feb 2013 18:12:00 +0000 /?p=516#comment-7543 52 is correct answer – 51 comprises only of 1 and 50..
whereas 52 is series of 1, 50, -20, 1, 20
in this problem you have to find the largest sum which is 52

]]>
By: rahul /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7523 Thu, 13 Dec 2012 09:45:00 +0000 /?p=516#comment-7523 void MaxSubSeq(int array[],int length, int *start, int *end)

{

long long max_sum = -(((long long)1)<<(8*sizeof(int)));

long long entire_sum=0;

int break_point = -1,i=0;

for(; i<length; i++)

{

entire_sum += array[i];

printf("entire_sum = %lldn",entire_sum);

if(max_sum < entire_sum)

{

max_sum = entire_sum;

printf("Max_Sum = %lld ",max_sum);

*start = break_point+1;

*end = i;

printf("start %d end %d n", *start, *end);

}

if(entire_sum <= 0)

{

entire_sum = 0;

break_point = i;

}

}

}

main () {

//int arr[] = {1, 3, 5, -2, 6, -2, -4, -8, 2, 3, -1, -1, 4, 5, 9};

int arr[] = {-3,-5,-2,-4,-6,-9,-1,-10};

int s = 1, e = 7;

MaxSubSeq(arr, 8, &s, &e);

}

]]>
By: Md /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7514 Fri, 07 Dec 2012 23:48:00 +0000 /?p=516#comment-7514 This is the right solution. Some test cases
{3, -5, 2, -4, 6, -9, 9, -10}; //positive negative pattern
{-5, 4, 7, -9, 1};
{-5, -4, -7, -9, -1}; all negative
{ 2, 3, -28, 4, 8, 1, 5, 0};
{ 5, 7, -13, 10, 5 }; //should return 15
{ 0 }; // should return 0
{999999999}; //edge case should return 999999999
{-40,1,40,-50,1,50,-20,1,20,0,0}
{ 2, -2, 5, -5, 6, -6 }; // should return 6
{-40,1,40,-50,1,50,-20,1,20,0,0}; //return 51

]]>
By: Mahmoud /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7513 Fri, 07 Dec 2012 23:19:00 +0000 /?p=516#comment-7513 Hi Prakash, I altered your code to handle some test cases. He it is.

]]>
By: Md /2011/09/24/programming-interview-questions-3-largest-continuous-sum/#comment-7511 Fri, 07 Dec 2012 19:31:00 +0000 /?p=516#comment-7511 thanks for the reply but your code gives 0 for input={3,-5,2,-4,6,-9,9,-10} and I think it should return 9

]]>