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 
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 

Input

5, 14

Output

3
Updated on: 2022-03-15T05:52:56+05:30

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements