C++ code to find minimum k to get more votes from students

Suppose we have an array A with n elements. There are n students in a school and each of them has exactly k votes and all votes should be used. There are two parties. The A[i] represents ith student has given A[i] amount of votes to first party and this implies the second party will get k- A[i] number of votes. The second party wants to set k in such a way that they win. What will be the minimum possible value of k.

So, if the input is like A = [2, 2, 3, 2, 2], then the output will be 5, because first party is getting 2 + 2 + 3 + 2 + 2 = 11 votes. if k = 5, then second party will get 3 + 3 + 2 + 3 + 3 = 14 votes and win the election.

Steps

To solve this, we will follow these steps −

n := size of A
for initialize k := 0, when k 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
int solve(vector A){
   int n = A.size(), k = 0, s = 0, m = 0;
   for (int k = 0; k  A = { 2, 2, 3, 2, 2 };
   cout 

Input

{ 2, 2, 3, 2, 2 }

Output

5
Updated on: 2022-03-29T11:33:28+05:30

316 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements