@Minotauraus: This is not the thing that "Raj Jagvanshi" wants. See, what he has mentioned. a[] = {4,1,2,3,4,5,40,41,19,20}; print = 40 , 41 sum = 81;
We need to find max sum such that they are of consecutive numbers (a,a +1,a+2,....) I have implemented Kadane's Algo : and the result is 4 1 2 3 4 5 40 41 19 20 4 1 2 3 4 5 40 41 19 20 Max : [139] which is wrong.... Here is the code of Kadane's Algorithm, u can check it on your own : #include<stdio.h> int main(){ int n; int max,a,b; int curr,aa,bb; int arr[1000]; int i; printf("Enter numbers (-1 to stop taking input) :"); for(i=0 ; i<1000 ; i++){ scanf("%d",&arr[i]); if(arr[i] == -1){ n=i; break; } } max = -99999; a = b = 0; curr = 0; aa = 0; for(bb=0 ; bb<n ; bb++){ curr = curr + arr[bb]; if(curr > max){ max = curr; a = aa; b = bb; } if(curr < 0){ curr = 0; aa = bb + 1; } } for(i=a ; i<=b ; i++){ printf("%d\t",arr[i]); } printf("\nMax : [%d]\n",max); return 0; } =================================================== On Sep 18, 10:29 pm, Minotauraus <anike...@gmail.com> wrote: > I was looking into this problem the other day and found > this:http://www.algorithmist.com/index.php/Kadane's_Algorithm > Kadane's algorithm to find the maximum continuous subarray. > > The basic idea is to maintain the largest sum ending at each location > in the array. and max which holds the maximum so far. > > On Sep 17, 11:41 am, Raj Jagvanshi <raj.jagvan...@gmail.com> wrote: > > > > > a[] = {4,1,2,3,4,5,40,41,19,20}; > > > print = 40 , 41 > > sum = 81; > > > give me code -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.