Header Ads Widget

Ceiling Sum | codechef solution

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

2+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=25=51.5=13=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 2+2.

Constraints

  • 1105
  • 1,109

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 222+222 =  0+ 0 = 0+0=0.

  • In the second test case, we can choose  to be 2. So sum is equal to 1122+212 =  4.5+ 0.5 = 5+1=6. There is no possible way to choose an integer  in the range [1,11] such that sum is greater than 6.

 Code(C++):-

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    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
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t=Integer.parseInt(br.readLine()),a,b;
        while(t-->0)
        {
         String s[]=br.readLine().split(" ");
         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:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-