- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ code to find maximum score we can assign to first student

Suppose we have an array A with n elements and a number m. There are n students giving an exam. The highest possible score is m. A[i] is the score of ith student. We can manipulate each students score, but the conditions must be satisfied. The score will not exceed m, all scores are integers and the average marks of all student does not change. If we want to maximize the first person's score what will be the highest possible score we can give.

So, if the input is like A = [1, 2, 3, 4]; m = 10, then the output will be 10, because the average is 2.5, we can set the scores [10, 0, 0, 0] where the average is same but first one's score is maximum.

## Steps

To solve this, we will follow these steps −

sum := 0 n := size of A for initialize j := 0, when j < n, update (increase j by 1), do: sum := sum + A[j] return minimum of m and sum

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int m){ int sum = 0; int n = A.size(); for (int j = 0; j < n; j++){ sum += A[j]; } return min(m, sum); } int main(){ vector<int> A = { 1, 2, 3, 4 }; int m = 10; cout << solve(A, m) << endl; }

## Input

{ 1, 2, 3, 4 }, 10

## Output

10

- Related Questions & Answers
- C++ code to find rank of student from score table
- Program to find maximum score we can get in jump game in Python
- C++ code to find maximum stones we can pick from three heaps
- Can we assign values to final arrays in Java?
- Program to find maximum score from removing stones in Python
- Program to find maximum score in stone game in Python
- C++ Program to find maximum score of bit removal game
- MongoDB aggregation / math operation to sum score of a specific student
- C++ code to count maximum hay-bales on first pile
- Can we assign a reference to a variable in Python?
- C++ code to find score of winning square on a square board
- Program to find maximum additive score by deleting numbers in Python
- Program to find maximum score of brick removal game in Python
- Program to find maximum score from performing multiplication operations in Python
- Program to find maximum score of a good subarray in Python