
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to count who have declined invitation
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
- Related Articles
- C++ code to count children who will get ball after each throw
- Why have the sales of PC (personal computers) declined?
- C++ code to find who cannot give sufficient candies
- C++ code to count number of unread chapters
- C++ code to count volume of given text
- C++ code to count operations to make array sorted
- C++ code to count days to complete reading book
- C++ code to count ways to form reconnaissance units
- C++ code to find out who won an n-round game
- C++ code to count copy operations without exceeding k
- C++ code to count order collected when client calls
- C++ code to count local extrema of given array
- C++ code to count maximum groups can be made
- C++ code to count maximum banknotes bank can gather
- C++ code to find maximum fruit count to make compote

Advertisements