C++ code to count who have declined invitation

C++Server Side ProgrammingProgramming

Suppose we have an array A with n elements, and all elements are distinct. There are n of the onsite finalists who can join a company, their qualifying ranks are present in array A. We have to find the minimum possible number of contestants that declined the invitation to compete onsite in the final round. There will be 25 person from which, few have accepted or few declined.

So, if the input is like A = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28], then the output will be 3, because 1st, 13th and 27th must have declined.

Steps

To solve this, we will follow these steps −

mx := 0
for initialize i := 0, when i < size of A, update (increase i by 1), do:
   mx := maximum of mx and A[i]
return maximum of mx - 25 and 0

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A){
   int mx = 0;
   for (int i = 0; i < A.size(); i++)
      mx = max(mx, A[i]);
   return max(mx - 25, 0);
}
int main(){
   vector<int> A = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
      24, 25, 26, 28 };
   cout << solve(A) << endl;
}

Input

{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28 }

Output

3
raja
Updated on 30-Mar-2022 14:11:40

Advertisements