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 substract 1 from it.
Each operation cost 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
Input :
First line of input contains T denoting number of test cases.
Next T lines contains space seperated integers A,B,C
Output :
For each test case, print a single line containing one integer — the minimum time require to change A,B,C into an arithmetic progression.
Arithmetic Progression - HackerEarth Problem Solution
In C -
#include<stdio.h>
#include<math.h>
int main()
{
long long int t,a,b,c,count;
int i;
scanf("%lld",&t);
for(i=0;i<t;i++)
{
scanf("%lld %lld %lld",&a,&b,&c);
count=abs((2*b-(a+c)));
if(count%2==0)
printf("%d\n",count/2);
else
printf("%d\n",count/2+1);
}
}
Arithmetic Progression - HackerEarth Problem Solution
In C++ -
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>> t;
while(t--){
int a,b,c;
cin>>a>>b>>c;
int k, count=0;
k= abs((2*b)-(a+c));
(k%2==0)?count=k/2:count=(k+1)/2;
cout<<count<<"\n";
}
}
Arithmetic Progression - HackerEarth Problem Solution
In Java -
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tests= Integer.parseInt(br.readLine());
StringBuilder out = new StringBuilder();
for(int x=0;x<tests;x++){
String arr[]=br.readLine().split(" ");
int a = Integer.parseInt(arr[0]);
int b= Integer.parseInt(arr[1]);
int c= Integer.parseInt(arr[2]);
int diff = (Math.abs((c-b)-(b-a))+1)/2;
out.append(diff+"\n");
}
System.out.println(out);
}
}
Arithmetic Progression - HackerEarth Problem Solution
In Python -
test_case=int(input())
output=""
i=0
while i<test_case:
i+=1
lst=[]
lst=input().split(' ')
mean=(float(lst[0])+float(lst[2]))/2
if mean == float(lst[1]):
output+="0\n"
elif mean >= float(lst[1]):
output+=str(int(mean)-int(lst[1])+int((mean-int(mean))*2))+"\n"
else:
output+=str(int(lst[1])-int(mean))+"\n"
print(output)
Post a Comment