- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 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 < n, update (increase k by 1), do: x := A[k] m := maximum of m and x s := s + x return maximum of m and (2 * s / n + 1)

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(vector<int> A){ int n = A.size(), k = 0, s = 0, m = 0; for (int k = 0; k < n; k++){ int x = A[k]; m = max(m, x); s += x; } return max(m, 2 * s / n + 1); } int main(){ vector<int> A = { 2, 2, 3, 2, 2 }; cout << solve(A) << endl; }

## Input

{ 2, 2, 3, 2, 2 }

## Output

5

- Related Articles
- C++ code to find minimum correct string from given binary string
- C++ code to find minimum number starting from n in a game
- C++ code to get minimum sum of cards after discarding
- C++ code to find minimum arithmetic mean deviation
- C++ code to find position of students after coding contest
- C++ code to find minimum different digits to represent n
- C++ code to find minimum stones after all operations
- C++ code to find minimum difference between concerts durations
- C++ code to find minimum operations to make numbers c and d
- C++ code to find minimum jump to reach home by frog
- C++ code to find minimum time needed to do all tasks
- C++ code to find minimum moves with weapons to kill enemy
- C++ Program to find minimum k for candy distribution
- C++ program to count number of minimum coins needed to get sum k
- C++ code to find greater number whose factor is k