We need to find minimum number of moves to make heights equal. In one move, can add a floor or remove a floor from a building. So to make two heights equal, we can adjust both. Essentially, we want to change N to some value X and M to same X. Moves needed = |N-X| + |M-X|. Minimizing over X. This is classic: sum of absolute distances minimized when X is any median. For two numbers, any X between N and M gives sum = |N-M|. Actually, if X is between N and M, then |N-X| + |M-X| = (X - N) + (M - X) = M - N if M >= N, or N - M. If X is outside, sum is larger. So minimal sum = |N-M|. But wait: can we adjust both buildings? Yes, in each move we can either add or remove a floor from a building. So the minimal number of moves to make them equal is indeed |N-M|. However, check: can we do it with fewer? For N=2, M=3, |2-3|=1, sample output 1. So correct.
But consider if both buildings can be changed simultaneously? But moves are defined as one move per operation, you can either add a floor to a building, or remove a floor from a building. That's one move each. So to adjust both, you need moves for each building's changes. But sum of absolute differences is the total number of changes. So that seems correct.
But there is nuance: Could we possibly do it by adjusting both to a different height? Yes, but that sum is |N-X|+|M-X|, minimized at |N-M|.
Thus answer is absolute difference. Since constraints up to 10^18, use 64-bit integers.
Now T up to 10^5, simple loop.
Thus solution: for each test case, read N, M, print |N-M|.
But ensure we use long long in C++.
Thus answer is simple.
But let's double-check: If we could remove a floor from one and add to the other? That's two moves. But the absolute difference requires only one change? Actually if N=2, M=3, we could remove one from M to make both 2: one move (remove from M). Or add one to N to make both 3: one move. So min moves = 1 = |2-3|. For N=5, M=5, difference 0, no moves needed. So correct.
Edge case: N, M up to 10^18, difference up to 10^18, still within 64-bit signed.
Thus answer.
But wait: The problem says "He could either add a floor to a building, or remove a floor from a building." It does not say he cannot do both in one move. So each move is a single operation on one building. So yes.
Thus final answer.
But why the problem is from CodeChef? Might be that it's too trivial, but maybe they want to think about absolute difference. However, careful: Could there be a scenario where adding a floor to one and removing from the other in the same move? Not allowed, moves are individual.
Thus solution: For each test case, output |N-M|.
But I'll elaborate with proof: The minimal moves is the absolute difference. Because you need to change at least one building's height by the difference. And you can achieve exactly that by only adjusting the taller building down (or shorter up). That yields moves = difference.
Thus answer.
Now the answer for the sample: 1.
Implementation in C++:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long N, M;
cin >> N >> M;
cout << abs(N - M) << endl;
}
return 0;
}
But careful: abs for long long: use llabs in C++11? Actually std::abs works for long long in C++11 and later? It's overloaded for integral types. But safer: use llabs or just (N>M)? N-M : M-N.
We'll just use (N > M ? N - M : M - N). Or include and use std::abs, but it's ambiguous if it's overloaded for long long in older compilers. But typical online judges (CodeChef) use modern compilers. Anyway.
Thus answer.
I'll produce final explanation. The minimum number of moves required is the absolute difference between the two heights.
You can either increase the shorter building or decrease the taller building, each operation changes the height by exactly one floor. Thus, to make them equal, you need to change the height of at least one building by the difference between the original heights. The optimal strategy is to change only one building, which takes exactly |N−M| moves.
Complexity
- Time: O(T) per test case.
- Space: O(1).
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
long long N, M;
cin >> N >> M;
// Compute absolute difference safely
long long diff = (N > M) ? N - M : M - N;
cout << diff << '\n';
}
return 0;
}