Skip to content

Instantly share code, notes, and snippets.

@ceoro9
Created October 28, 2017 11:07
Show Gist options
  • Save ceoro9/e387d56fbf96ec4b01911382ac97882b to your computer and use it in GitHub Desktop.
Save ceoro9/e387d56fbf96ec4b01911382ac97882b to your computer and use it in GitHub Desktop.
minimum value on sub-section
#include <iostream>
#define INF 2147483647
using namespace std;
const int N = 8;
int a[N] = {4,5,-4,3,-4,-20,4,-5};
int main() {
int ans = -INF,
ans_l = 0,
ans_r = 0,
sum = 0,
min_pos = 0,
min_sum = 0;
for (int r = 0; r < N; ++r) {
sum += a[r];
if (sum > ans) {
ans = sum;
ans_l = min_pos;
ans_r = r;
}
if (sum < min_sum) {
min_sum = sum;
min_pos = r;
}
}
for (int i = ans_l; i <= ans_r; ++i) {
cout << a[i] << " ";
}
cout << endl;
cout << "resultSum = " << ans << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment