- 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 count minimum button click to set volume as before
Suppose we have two numbers a and b. Amal always sets TV volume to 'b' value. But someday Bimal has changed it to 'a' value. The remote has six buttons (-5, -2, -1, 1, 2, 5) using them we can increase or decrease the volume by 1, 2 or 5. Volume can be very large but not negative. We have to count the number of buttons Amal needs to press at minimum to get the volume same as b.
So, if the input is like a = 5; b = 14, then the output will be 3, because press +5 to get 10, then +2 twice to get 14.
Steps
To solve this, we will follow these steps −
d := |a - b| return (d / 5 + (d mod 5 + 1) / 2)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int a, int b){ int d = abs(a - b); return (d / 5 + (d % 5 + 1) / 2); } int main(){ int a = 5; int b = 14; cout << solve(a, b) << endl; }
Input
5, 14
Output
3
Advertisements