Header Ads Widget

Cricket Rating

Problem:-

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 positive rating is awarded to the cricketer.Chang wants to analyse the performance of a cricketer over a period of N matches. Chang wants to find 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.
Input
The first line contain number of matches "N" over which the analysis is to be done. The second line contains those ratings of a batsman/bowler in those N matches.
Output
Print a single integer ie. the maximum consistent sum of rating of the cricketer if it is positive otherwise output 0 (zero).
Constraint
0N(matches)105
100rating+100
SAMPLE INPUT
 
8
-1 -4  4 -2 0 1 4 -5
SAMPLE OUTPUT
 
7
Explanation
here the maximum consistent and continuous sum of rating is 4+(2)+0+1+4=7
Time Limit:3.0 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:1024 KB

solution:-

#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,a[100000],i,sum=0,max=-32670;
    scanf("%d",&n);
    for(i=0;i<=n-1;i++)
     scanf("%d",&a[i]);
for(i=0;i<=n-1;i++)
    {
        sum=sum+a[i];
        if(max<sum)
         max=sum;
        if(sum<0)
         sum=0; 
    }
    if(max>0)
     printf("%d",max);
else
     printf("0");
}

Post a Comment

0 Comments