# Ceiling Sum | codechef solution

You are given two integers $�,�$. You have to choose an integer $�$ in the range [minimum($�,�$), maximum($�,�$)] such that:

$⌈\frac{�-�}{2}⌉+⌈\frac{�-�}{2}⌉$ is maximum.

What is the maximum sum you can obtain if you choose $�$ optimally?

Note: $⌈�⌉$ : Returns the smallest integer that is greater than or equal to $�$ (i.e rounds up to the nearest integer). For example, $⌈1.4⌉=2$$⌈5⌉=5$$⌈-1.5⌉=-1$$⌈-3⌉=-3$ , $⌈0⌉=0$.

### Input Format

• First line will contain $�$, number of testcases. Then the testcases follow.
• Each testcase contains of a single line of input, two integers $�,�$.

### Output Format

For each testcase, output the maximum sum of $⌈\frac{�-�}{2}⌉+⌈\frac{�-�}{2}⌉$.

### Constraints

• $1\le �\le 1{0}^{5}$
• $1\le �,�\le 1{0}^{9}$

### Sample 1:

Input
Output
3
2 2
1 11
13 6

0
6
-3

### Explanation:

• In the first test case, there is only one possible value of $�$ which is equal to $2$. So the sum is equal to $⌈\frac{2-2}{2}⌉+⌈\frac{2-2}{2}⌉$ =  = $0+0=0$.

• In the second test case, we can choose $�$ to be $2$. So sum is equal to $⌈\frac{11-2}{2}⌉+⌈\frac{2-1}{2}⌉$ =  = $5+1=6$. There is no possible way to choose an integer $�$ in the range $\left[1,11\right]$ such that sum is greater than $6$.

Code(C++):-

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int a,b;
cin>>a>>b;
if(a==b)cout<<"0"<<endl;
else if(b>a)cout<<((b-a)/2)+1<<endl;
else if((a-b)%2==0){
cout<<((b-a)/2)+1<<endl;

}else cout<<(b-a)/2<<endl;
}
return 0;
}

Code(JAVA):-

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
while(t-->0)
{
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
if(b!=a)
System.out.println(1+(((b-1-a)%2==0)?(b-1-a)/2:(b-a)/2));
else
System.out.println(0);
}
}
}

### Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-