Calculate the biggest sum of subarray

 Given an array of integers, how to calculate the biggest sum of its subarrays? This is a typical problem and is solved in almost every textbook about algorithm, especially about dynamic programming. However, in most cases, there is always an extra requirement that the biggest sum will be ZERO if the final result is negative. This leads to a limitation of the solution. For example, if all the data in the array are negative, then the real answer (i.e., the biggest negative integer in it) is hidden by ZERO. The below code can deal with this situation. The basic idea is still dynamic programming, that is, if the first i elements' biggest sum is known, when considering the (i + 1)th element, there will be only two possible consequences: one is that the (i + 1)th element can be added to the "biggest" subarray, while the other is that a new "bigger" subarray, which contains only the (i + 1)th element is taking place of the old one. During the whole process, the starting and the ending index of the "biggest" subarray can be maintained. Here is one version of implementation (Using the test data in <Beauty of Programming>).

  1. #include <iostream>
  2. using namespace std;
  3. void max_sum_of_subarray(int* A, int n, int& max_all, int& start_index, int& end_index) {
  4.     int max_pre; // Store the max sum of the pre-subarray.
  5.     int temp_start_index, temp_end_index; // Bookkeeping during the process.
  6.     max_pre = max_all = A[0];
  7.     temp_start_index = temp_end_index = start_index = end_index = 0;
  8.     for(int i = 1; i < n; ++i) {
  9.         if(A[i] > max_pre + A[i]) { // A new "bigger" subarray appears.
  10.             max_pre = A[i];
  11.           temp_start_index = temp_end_index = i;
  12.         }
  13.         else { // Just add the ith element into the old subarray.
  14.             max_pre += A[i];
  15.             temp_end_index = i;
  16.         }
  17.         if(max_all < max_pre) { // Adjust the final max sum and the indices.
  18.             max_all = max_pre;
  19.             start_index = temp_start_index;
  20.             end_index = temp_end_index;
  21.         }
  22.     }
  23. }
  24. int main() {
  25.         // Test data from <Beauty of Programming>.
  26.     int a[6] = {1, -2, 3, 5, -3, 2};
  27.     int b[6] = {0, -2, 3, 5, -1, 2};
  28.     int c[5] = {-9, -2, -3, -5, -3};
  29.     
  30.     int max_sum, start_index, end_index;
  31.     max_sum_of_subarray(a, 6, max_sum, start_index, end_index);
  32.     cout << "max sum: " << max_sum << "/trange: [" << start_index << ", " << end_index << "]" << endl;
  33.     max_sum_of_subarray(b, 6, max_sum, start_index, end_index);
  34.     cout << "max sum: " << max_sum << "/trange: [" << start_index << ", " << end_index << "]" << endl;
  35.     max_sum_of_subarray(c, 5, max_sum, start_index, end_index);
  36.     cout << "max sum: " << max_sum << "/trange: [" << start_index << ", " << end_index << "]" << endl;
  37.     
  38.     return 0;
  39. }

The command line outputs:

max sum: 8        range: [2, 3]

max sum: 9        range: [2, 5]

max sum: -2       range: [1, 1]

 

Referenced to <Beauty of Programming>.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值