Created
October 28, 2017 11:07
-
-
Save ceoro9/e387d56fbf96ec4b01911382ac97882b to your computer and use it in GitHub Desktop.
minimum value on sub-section
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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