|
| 1 | +/* |
| 2 | +==================================================== |
| 3 | +PROBLEM STATEMENT: |
| 4 | +==================================================== |
| 5 | +There are n people standing in a circle (n is even). |
| 6 | +Each person looks at the person exactly opposite to them. |
| 7 | +
|
| 8 | +You are given three distinct integers a, b, and c. |
| 9 | +It is known that person 'a' is looking at person 'b'. |
| 10 | +
|
| 11 | +Determine the person that 'c' is looking at. |
| 12 | +If multiple answers exist, print any. |
| 13 | +If no valid answer exists, print -1. |
| 14 | +
|
| 15 | +---------------------------------------------------- |
| 16 | +PROBLEM LINK: |
| 17 | +https://codeforces.com/problemset/problem/1560/B |
| 18 | +==================================================== |
| 19 | +*/ |
| 20 | + |
| 21 | +/* |
| 22 | +==================================================== |
| 23 | +APPROACH: |
| 24 | +==================================================== |
| 25 | +If person 'a' is looking at person 'b', then: |
| 26 | +- The distance between them is |a - b| |
| 27 | +- Since they are opposite, total number of people: |
| 28 | + n = 2 * |a - b| |
| 29 | +
|
| 30 | +Steps: |
| 31 | +1. Compute dist = |a - b| |
| 32 | +2. Compute n = 2 * dist |
| 33 | +3. If a, b, or c is greater than n → invalid → print -1 |
| 34 | +4. Otherwise, the person opposite to c is: |
| 35 | + - c + dist (if within n) |
| 36 | + - else c - dist |
| 37 | +
|
| 38 | +---------------------------------------------------- |
| 39 | +WHY THIS WORKS: |
| 40 | +- In a circular arrangement, opposite positions differ |
| 41 | + by exactly n/2 positions. |
| 42 | +==================================================== |
| 43 | +*/ |
| 44 | + |
| 45 | +/* |
| 46 | +==================================================== |
| 47 | +TIME & SPACE COMPLEXITY: |
| 48 | +==================================================== |
| 49 | +Time Complexity: O(1) per test case |
| 50 | +Space Complexity: O(1) |
| 51 | +==================================================== |
| 52 | +*/ |
| 53 | + |
| 54 | +/* |
| 55 | +==================================================== |
| 56 | +EXAMPLE: |
| 57 | +==================================================== |
| 58 | +Input: |
| 59 | +1 |
| 60 | +1 3 2 |
| 61 | +
|
| 62 | +Output: |
| 63 | +4 |
| 64 | +
|
| 65 | +Explanation: |
| 66 | +|1 - 3| = 2 → n = 4 |
| 67 | +Opposite of 2 is 2 + 2 = 4 |
| 68 | +==================================================== |
| 69 | +*/ |
| 70 | + |
| 71 | +/* |
| 72 | +==================================================== |
| 73 | +SUBMISSION LINK: |
| 74 | +==================================================== |
| 75 | +https://codeforces.com/contest/1560/submission/345095137 |
| 76 | +==================================================== |
| 77 | +*/ |
| 78 | + |
| 79 | +#include <bits/stdc++.h> |
| 80 | +using namespace std; |
| 81 | + |
| 82 | +#define fastio() ios::sync_with_stdio(false); cin.tie(nullptr); |
| 83 | + |
| 84 | +int main() { |
| 85 | + fastio(); |
| 86 | + |
| 87 | + int t; |
| 88 | + cin >> t; |
| 89 | + |
| 90 | + while (t--) { |
| 91 | + long long a, b, c; |
| 92 | + cin >> a >> b >> c; |
| 93 | + |
| 94 | + long long dist = llabs(a - b); |
| 95 | + long long n = 2 * dist; |
| 96 | + |
| 97 | + if (a > n || b > n || c > n) { |
| 98 | + cout << -1 << '\n'; |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if (c + dist <= n) |
| 103 | + cout << c + dist << '\n'; |
| 104 | + else |
| 105 | + cout << c - dist << '\n'; |
| 106 | + } |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
0 commit comments