Algorithm


A. Three Friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Three friends are going to meet each other. Initially, the first friend stays at the position x=a�=�, the second friend stays at the position x=b�=� and the third friend stays at the position x=c�=� on the coordinate axis Ox��.

In one minute each friend independently from other friends can change the position x by 11 to the left or by 11 to the right (i.e. set x:=x1�:=�−1 or x:=x+1�:=�+1) or even don't change it.

Let's introduce the total pairwise distance — the sum of distances between each pair of friends. Let a�′b�′ and c�′ be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is |ab|+|ac|+|bc||�′−�′|+|�′−�′|+|�′−�′|, where |x||�| is the absolute value of x.

Friends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute.

You have to answer q independent test cases.

Input

The first line of the input contains one integer q (1q10001≤�≤1000) — the number of test cases.

The next q lines describe test cases. The i-th test case is given as three integers a,b�,� and c (1a,b,c1091≤�,�,�≤109) — initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.

Output

For each test case print the answer on it — the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after one minute.

Example
input
Copy
8
3 3 4
10 20 30
5 5 5
2 4 3
1 1000000000 1000000000
1 1000000000 999999999
3 2 5
3 2 6
output
Copy
0
36
0
0
1999999994
1999999994
2
4

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

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


#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  while(t--){
    int a[3];
    cin>>a[0]>>a[1]>>a[2];
    sort(a, a+3);
    if(a[0] == a[1] && a[2] != a[1]){
      if(a[2] - a[1] > 1){
        a[2] -= 1;
      }
      a[0] = a[0] + 1;
      a[1] = a[1] + 1;
    }
    else if(a[2] == a[1] && a[0] != a[1]){
      if(a[1] - a[0] > 1){
        a[0] += 1;
      }
      a[2] = a[2] - 1;
      a[1] = a[1] - 1;
    }
    else if(a[2] != a[1] && a[0] != a[1]){
      a[0] += 1;
      a[2] -= 1;
    }
    int dista = (a[0] - a[1] >= 0) ? (a[0] - a[1]) : (a[1] - a[0]);
    int distb = (a[1] - a[2] >= 0) ? (a[1] - a[2]) : (a[2] - a[1]);
    int distc = (a[0] - a[2] >= 0) ? (a[0] - a[2]) : (a[2] - a[0]);
    cout<<(dista + distb + distc)<<endl;
  }

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
8
3 3 4
10 20 30
5 5 5
2 4 3
1 1000000000 1000000000
1 1000000000 999999999
3 2 5
3 2 6

Output

x
+
cmd
0
36
0
0
1999999994
1999999994
2
4
Advertisements

Demonstration


Codeforcess solution 1272-A A. Three Friends C++, Java, Js and Python,1272-A,Codeforcess solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+