Source Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<assert.h>
#include<memory.h>
#include<cmath>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;


void solve(int t) {
  int n, k;
  cin >> n >> k;
  
  if (k > n * 2 || k == 1)return void(cout << 0);

  if (k - 1 < n) {
    int g = n - (k-1);
    return void(cout << ((n-g) + 1) / 2);
  }

  int g = k - n;
  cout << (n - g + 2) / 2;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
     cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    solve(tt);
    cout << '\n';
  }
}


Copy
Ramadan Candies iyaad
GNU G++17
6 ms
376 KB
Accepted