In particular, when all integers in a sequence are negative integers, their maximum is 0.
1#include <stdio.h>2 3 intCalumaxsubsum (int*array,intLeftintRight );4 5 intMain ()6 { 7 intarray[6] = {2, -2,3,1, -4,2};8 intLen_array =sizeof(array)/sizeof(array[0]);9 inti =0;Ten intSubsum = calumaxsubsum (Array,0, Len_array); One for(i =0; i < Len_array; i++) A { -printf"%d", Array[i]); - } the -printf"\ n"); - //printf ("Max Son and yes:"); -printf"subsum =%d\n", subsum); + return 0; - } + A intCalumaxsubsum (int*array,intLeftintRight ) at { - intsum =0; - if(Left >=Right ) - { - if(Array[left] >0) - { insum =Array[left]; - } to Else + { -sum =0; the } * } $ ElsePanax Notoginseng { - intCenter = (left + right)/2; the intS1 =0, S2 =0; + intLefts =0, rights =0; A inti =0; the intLeftsum =calumaxsubsum (array, left, center); + intRightsum = calumaxsubsum (array, center +1, right); - for(i = center; I >= left; i--) $ { $Lefts + =Array[i]; - if(Lefts >S1) - { theS1 =lefts; - }Wuyi } the - for(i = center +1; I < right; i++) Wu { -Rights + =Array[i]; About if(Rights >S2) $ { -S2 =Rights; - } - } A +sum = S1 +S2; the if(Sum <leftsum) - { $sum =leftsum; the } the if(Sum <rightsum) the { thesum =rightsum; - } in } the the returnsum; About}
Where is the key idea?
Solving the maximal sub-problems of sequences by divide-and-conquer algorithm