We are given: "maximum consistent sum of cricket rating of a batsman or a bowler only if his overall rating is positive over that period." Let's parse the problem statement.
We have N matches, each with a rating (integer between -100 and 100). We need to find the maximum consistent sum of rating of the cricketer if it is positive; otherwise output 0.
But "consistent" is ambiguous. Usually, "consistent sum" might mean a contiguous subarray (continuous segment) sum. Also "only if his overall rating is positive over that period" suggests that we consider only those contiguous periods (subarrays) for which the overall rating (i.e., sum of that subarray) is positive. Among all such positive-sum contiguous subarrays, we need the maximum sum.
However, the sample: N=8, ratings = [-1, -4, 4, -2, 0, 1, 4, -5]. Let's compute all contiguous subarrays and their sums. They say "maximum consistent and continuous sum" is 4 + (-2) + 0 + 1 + 4 = 7. Indeed, that subarray is from index 3? Actually 0-index: positions: 1: -1, 2: -4, 3: 4, 4: -2, 5: 0, 6: 1, 7: 4, 8: -5. The subarray from 3 to 7 (4, -2, 0, 1, 4) sum = 4-2+0+1+4 = 7. Are there any larger positive sum? Consider subarray 3 to 7 (4, -2, 0, 1, 4) = 7. Maybe also subarray 3 to 8: 4 -2 +0+1+4-5 = 2. Subarray 6 to 7: 1+4=5. Subarray 3 to 3: 4, sum=4. Subarray 6 to 8: 1+4-5=0 (non-positive). Subarray 1 to 8 sum: -1-4+4-2+0+1+4-5 = -3? Actually compute: -1-4=-5, +4=-1, -2=-3, +0=-3, +1=-2, +4=2, -5=-3. So -3, negative. So the maximum sum subarray (classic Kadane's maximum subarray sum) would be? Let's compute: if we run Kadane's, the maximum sum contiguous subarray might be 4, -2, 0, 1, 4 = 7. But maybe also consider 4 alone =4, 4, -2,0,1,4 =7, 1+4=5. Could there be 4+ (-2) +0+1+4 =7. Also what about -1-4+4-2+0+1+4? That's -1-4=-5+4=-1-2=-3+0=-3+1=-2+4=2, sum=2. So 7 is maximum. So the problem reduces to: maximum subarray sum (contiguous) that is positive? Actually Kadane's max sum can be negative if all numbers negative, but then they'd output 0 per problem. But here they say: "only if his overall rating is positive over that period." That could be interpreted as: We need to find the maximum sum of a contiguous segment, but we only consider segments that have a positive total sum. If no segment has positive sum, output 0. That is exactly the classic maximum subarray sum problem, but with the twist that if the maximum sum is negative, we output 0. However, the classic Kadane's gives the maximum sum (which could be negative). So you can compute max subarray sum, and if it's >0, output that; else output 0.
But is there any nuance about "consistency"? Could "consistent" mean something else like "non-decreasing rating"? No, the sample explains "maximum consistent and continuous sum". So consistent likely means "continuous" (i.e., contiguous). So it's exactly that.
Let's check constraints: N up to 1e5, rating range -100 to 100. So O(N) solution works.
But then why do they say "only if his overall rating is positive over that period"? Possibly it's a condition: we must consider only those periods (subarrays) where the sum is positive. So again matches.
Thus answer: apply Kadane's algorithm to find maximum subarray sum. If sum > 0, output sum; else output 0.
But wait, the sample output is 7, which matches.
But is there any scenario where we need to consider something else? For instance, maybe they want the sum over the entire period if it's positive? No, it says "maximum consistent sum" which suggests we can choose any contiguous subsequence.
Thus solution is straightforward.
But double-check: The problem might be from HackerEarth (sample might be typical). Indeed it's like "Cricket Rating" problem from CodeChef? Actually I recall a problem "Cricket Rating" on CodeChef: "NATR" maybe. But I think it's from HackerEarth, maybe "Cricket Rating". Usually it's exactly maximum subarray sum.
Thus answer: We need to implement solution reading N and array, compute max subarray sum (Kadane), if >0 print that else 0.
Edge Cases: N can be 0? The constraint says 0 ≤ N ≤ 10^5. If N=0, no ratings. Should output 0. So handle that.
Also note: input may have spaces and newlines. Use standard input parsing.
Complexity O(N) time, O(1) extra.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long max_ending_here = 0, max_so_far = 0;
bool any_positive = false;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
max_ending_here += x;
if (max_ending_here < 0) max_ending_here = 0;
if (max_ending_here > max_so_far) {
max_so_far = max_ending_here;
any_positive = true;
}
}
if (any_positive) cout << max_so_far << endl;
else cout << 0 << endl;
return 0;
}
But note: This variant of Kadane's works for non-negative max, resetting to 0 when negative. This will give 0 if all numbers are negative. But we need to output 0 if no positive sum subarray exists. That matches. However, careful: if all numbers are negative, max_ending_here will become negative then reset to 0, and max_so_far will remain 0. So output 0. Good.
But what about a mix where the maximum subarray sum is positive but there may be zero-sum? output positive. Works.
But there is nuance: If the maximum sum is positive but we reset to 0 only when negative, that's okay because we track max_so_far. But if the sum becomes negative, we set to 0, effectively discarding that subarray. This works only if we are okay with max sum being positive. But if all numbers are negative, max_so_far stays 0. But what if there is a subarray with sum 0? Should we consider 0 as positive? The problem says "only if his overall rating is positive over that period." So sum must be positive, not zero. So 0 should not be considered. So output 0 if no positive sum. So algorithm is fine.
But one more nuance: Could the maximum sum be positive but there exists a subarray with sum 0? Not relevant.
Thus simple.
But let's ensure we treat integer overflow: N up to 1e5, each rating up to 100 absolute, so maximum sum could be up to 1e5 * 100 = 1e7, fits in int (2^31 ~2.1e9), but use long for safety.
Now, is there any alternative interpretation? Could "consistent" refer to something like "non-negative ratings"? No.
Maybe "consistency" means "monotonic"? The sample would not work.
Thus answer is Kadane.
Given the problem statement: "maximum consistent sum of cricket rating of a batsman or a bowler only if his overall rating is positive over that period." This seems like a maximum subarray problem.
Thus code as above.
But in some problems, they might ask for the maximum sum of a subarray that has a positive total sum, which is same.
Thus solution.
Now, implement in required language: typically C++ is acceptable. But the platform may require any language. But we can present explanation and code.
Thus final answer: Provide explanation and code.
But we must also think: Could there be a nuance about "overall rating is positive over that period" meaning that the sum of the entire N matches is positive? That would be different: They might want the maximum contiguous sum but only if the sum over the whole N is positive. However, the phrasing: "only if his overall rating is positive over that period." The phrase "that period" likely refers to the period we are considering (the subarray). So it's saying for a subarray, if its overall rating is positive, then we consider its sum. So among all such, find maximum. So it's exactly what we did.
Another reading: Perhaps they want to consider the entire period of N matches, and we are to find the maximum sum of a contiguous segment, but only if the cricketer's overall rating (the total over N matches) is positive. That would be a different condition: First check if total sum over N is > 0; if yes, then find max subarray sum; else output 0. Let's test with sample: total sum = -3? Actually we computed total sum? earlier: -1-4+4-2+0+1+4-5 =? Let's compute precisely: -1-4 = -5; +4 = -1; -2 = -3; +0 = -3; +1 = -2; +4 = 2; -5 = -3. So total = -3. If condition was total > 0, we would output 0. But sample output is 7, not 0. So that reading is invalid.
Thus our interpretation is correct.
So answer is Kadane.
Now, provide solution.
Given that we are to help Chang, we can present solution.
But wait: The sample input has extra spaces: "-1 -4 4 -2 0 1 4 -5". Our code with cin will parse fine.
Thus final.
I'll write explanation and code.
But also note: we can do a more direct Kadane: max_ending_here = max(x, max_ending_here + x) and then max_so_far = max(max_so_far, max_ending_here). That variant gives the actual maximum sum even if all negative. Then we would output max_so_far if >0 else 0. That works as well.
Implementation:
int max_ending_here = a[0], max_so_far = a[0];
for i=1..n-1:
max_ending_here = max(a[i], max_ending_here + a[i]);
max_so_far = max(max_so_far, max_ending_here);
if (max_so_far > 0) cout << max_so_far; else cout << 0;
This works for n>=1. For n=0, output 0.
But careful: If all numbers are negative, max_so_far will be negative. So output 0. Good.
Thus either approach works.
I'll choose the first approach because it's simpler and yields 0 for all negative.
Let's produce final answer.
But also note: Input size up to 1e5, we can read line.
We'll present C++ solution.
Thus final answer. We need to find the maximum sum of a contiguous subarray (consistent and continuous segment) whose sum is positive. If no such subarray exists, output 0.
This is essentially the maximum subarray sum problem (Kadane's algorithm) with the extra condition that we only consider positive sums; if the maximum sum is negative or zero, we output 0.
Algorithm:
- Read
N and the array of ratings.
- Initialize
max_ending_here = 0 and max_so_far = 0. Also keep a flag any_positive = false to know if we ever saw a positive sum (though max_so_far > 0 would also indicate it).
- For each rating
x:
- Add
x to max_ending_here.
- If
max_ending_here becomes negative, reset it to 0 (since any further extension would only be worse and we only care about positive sums).
- If
max_ending_here is greater than max_so_far, update max_so_far and set any_positive = true.
- After processing all numbers, if
max_so_far > 0 (or any_positive is true) output max_so_far; otherwise output 0.
Time Complexity: O(N)
Space Complexity: O(1)
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long max_ending_here = 0, max_so_far = 0;
bool any_positive = false;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
max_ending_here += x;
if (max_ending_here < 0)
max_ending_here = 0;
if (max_ending_here > max_so_far) {
max_so_far = max_ending_here;
any_positive = true;
}
}
if (any_positive)
cout << max_so_far << endl;
else
cout << 0 << endl;
return 0;
}