Arithmetic Progression

You will be given three numbers A, B, C . You can perform the following operation on these numbers any number of times. You can take any integer from A, B, C and you can add or subtract 1 from it.

Each operation costs 1sec of time(say). Now you have to determine the minimum time required to change those numbers into an Arithmetic Progression. 

i.e B-A=C-B

Example:

Input:  a=-5, b=7, c=6
Output: 7

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long arithProgression(long long along long blong long c)
{
    long long ans = abs(2 * b - (a + c));
    return (ans + 1) / 2;
}
int main()
{
    long long a = -5b = 7c = 6;

    cout << arithProgression(abc);

    return 0;
}


No comments:

Post a Comment