PROBLEM 93
Hard

Nearest of Two

Programming Basics · C++

</>
STATUS Not solved

TASK

Problem

#93

Given integers `x`, `a`, and `b`, print whichever of `a` or `b` is closer to `x`; if the distances are equal, print `a`.

EXAMPLE

Example

Input
5 2 9
Output
2

LIMITS

Constraints

|x|, |a|, |b| ≤ 10^9.
📖
LEARN Theory for this problem
+

These exercises combine arithmetic, comparisons, and small branches. Keep each condition explicit and easy to verify.

Connection to “Nearest of Two”: here, intermediate values are best kept in named variables so the formula stays readable and data types remain clear.

💡
NEED HELP? Hints
+

['Break the task into a few simple checks instead of compressing everything into a clever expression.', 'After solving “Nearest of Two”, verify the algorithm on your own small example and on an allowed boundary case. Print only the required result with no extra text.']

ANSWER Solution
+
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
    long long x,a,b;
    cin >> x >> a >> b;
    cout << (llabs(x-a)<=llabs(x-b)?a:b) << '\n';
    return 0;
}