Source Code
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <math.h>
#include <iomanip>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <array>
#include <set>
using namespace std;

int main()
{
    int _;
    cin>>_;
    while (_--)
    {
        long long n,k,ans=0;
        cin>>n>>k;
        int x= k/2;
        if(k==1||x>n)
        {
            cout<<0<<endl;
        }
        else if(x<=n/2)
        {
            cout<<x<<endl;
        }
        else
        {
            if(k%2==0)
            {
                cout<<n+1-x<<endl;
            }
            else
                cout<<n-x<<endl;
        }

    }
}
Copy
Ramadan Candies Abbro
GNU G++17
42 ms
312 KB
Accepted