- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 how many upgrade to make HP greater than current
Suppose we have a number n. In a game, every character has four different health points (HP). The categories are as follows −
Category A : If HP is in the form (4n + 1)
Category B : If HP is in the form (4n + 3)
Category C : If HP is in the form (4n + 2)
Category D : If HP is in the form 4n
These 4 categories ordered from highest to lowest as A > B > C > D. So, category A is the highest and category D is the lowest. While playing the game, players can increase the HP of the character. Now, Amal wants you to increase his HP by at most 2 (that is, either by 0, 1 or 2). We have to find how much should he increase his HP so that it has the highest possible category?
So, if the input is like n = 98, then the output will be 1 B, because 98 is in category C as (4*24 + 2), by increasing it by 1, it will be upgraded to category B, but if we increase it to 2, it will be 100 (4*25) which is category D. So at max category B is possible.
Steps
To solve this, we will follow these steps −
if n mod 4 is same as 2, then: return "1 B" Otherwise return |(n mod 4) - 1| and 'A'
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int n){ if (n % 4 == 2) cout << "1 B"; else cout << abs(n % 4 - 1) << " A"; } int main(){ int n = 98; solve(n); }
Input
98
Output
1 B