Surojit is an awesome cricketer of JGEC. His passion for the game & his talents are well known to everyone. Now he has a very weird hobby. He collects all the balls in each of the matches he has played and stores them in cylindrical containers one ball on top of the other, thus forming a “stack” of balls.
So, he has many such stack of balls kept in his room. Each stack has height Hi , denoting the number of balls in that particular stack. One fine day, Surojit decides to play a small game. This game consists of many number of rounds. In each round, he can select any number of consecutive stack of balls, such that number of balls selected is maximum. But wait, there’s a catch! The height of the stacks must be at most M. Now help Surojit to select maximum number of balls. You have the liberty to play any number of rounds.
Example:
Input: n = 7, m = 2, arr = {4, 2, 3, 5, 1, 1, 1}
Output: 3
Approach
C++
#include <bits/stdc++.h>using namespace std;long long maximumBalls(long long n, long long m,long long arr[]){long long res = -1, ans = 0;for (long long i = 0; i < n; i++){if (arr[i] <= m){ans += arr[i];}else{res = max(res, ans);ans = 0;}}res = max(res, ans);return res;}int main(){long long n = 7, m = 2;long long arr[n] = {4, 2, 3, 5, 1, 1, 1};cout << maximumBalls(n, m, arr) << "\n";return 0;}
No comments:
Post a Comment