Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- int maxSubArrSum(int v[], int n)
- {
- int maxi = INT_MIN, currmax = 0;
- bool haspos = false;
- for (int i = 0; i < n; i++)//daca nu exista elem pozitiv
- {
- maxi = max(maxi, v[i]);
- if (v[i] > 0)
- haspos = true;
- if (!haspos && i == n - 1)
- return maxi;
- }
- maxi = INT_MIN;
- for (int i = 0; i < n; i++)
- {
- currmax += v[i];
- if (currmax < 0)
- currmax = 0;
- else if (currmax > maxi)
- maxi = currmax;
- }
- return maxi;
- }
- int minSubArrSum(int v[], int n)
- {
- int mini = INT_MAX, currmin = 0;
- bool hasneg = false;
- for (int i = 0; i < n; i++)
- {
- mini = min(mini, v[i]);
- if (v[i] < 0)
- hasneg = true;
- if (!hasneg && i == n - 1)
- return mini;
- }
- mini = INT_MAX;
- for (int i = 0; i < n; i++)
- {
- currmin += v[i];
- if (currmin > 0)
- currmin = 0;
- else if (currmin < mini)
- mini = currmin;
- }
- return mini;
- }
- int main()
- {
- int v[]{8 ,-5 ,1 ,-3 ,7 ,-3 ,2 ,8 ,-3 ,1}, n = 10;
- cout << maxSubArrSum(v, n) << endl << minSubArrSum(v, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement