Cricket Rating

India is a cricket-crazy nation. Chang also loves cricket and computations related to cricket. Chang has created a Cricket app. This app analyses the performance of a cricketer. If a cricketer under-performs, then a negative rating is awarded. If performance is good, then a positive rating is awarded to the cricketer. Chang wants to analyze the performance of a cricketer over a period of N matches. Chang wants to find the consistency of a cricketer. So he wants to find out the maximum consistent sum of cricket rating of a batsman or a bowler only if his overall rating is positive over that period. Help chang in doing so.

Example:

Input: n = 8, arr[n] = {-1, -4, 4, -2, 0, 1, 4, -5}
Output: 7

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int kadanes(int arr[], int n)
{
    int max_sum = 0sum = 0;
    for (int i = 0i < ni++)
    {
        sum += arr[i];
        if (sum < 0)
            sum = 0;
        if (max_sum < sum)
            max_sum = sum;
    }
    return max_sum;
}
int main()
{
    int n = 8;

    int arr[n] = {-1, -44, -2014, -5};

    int ans = kadanes(arrn);
    cout << ans << "\n";

    return 0;
}


No comments:

Post a Comment