|
| 1 | +// 1. Problem Statement ------------------------------------------------- |
| 2 | +/* |
| 3 | +There are n people standing in a circle, where n is even. |
| 4 | +Each person looks at the person standing exactly opposite to them. |
| 5 | +
|
| 6 | +You are given three distinct integers a, b, and c. |
| 7 | +It is known that person a is looking at person b. |
| 8 | +
|
| 9 | +Determine the person that person c is looking at. |
| 10 | +If such a person cannot exist, print -1. |
| 11 | +*/ |
| 12 | + |
| 13 | +// 2. Approach ----------------------------------------------------------- |
| 14 | +/* |
| 15 | +The pair (a, b) fixes the entire circle. |
| 16 | +Once the circle is fixed, every person has exactly one opposite person. |
| 17 | +
|
| 18 | +If person c does not fit into this fixed circle, the answer is -1. |
| 19 | +Otherwise, move half the circle from c to find the person they are looking at. |
| 20 | +*/ |
| 21 | + |
| 22 | +// 3. Complexity --------------------------------------------------------- |
| 23 | +/* |
| 24 | +Time Complexity: O(1) per test case |
| 25 | +Space Complexity: O(1) |
| 26 | +*/ |
| 27 | + |
| 28 | +//4. problem submission link----------------------------------- |
| 29 | +// https://codeforces.com/problemset/status?my=on |
| 30 | + |
| 31 | +// Compiling Code in Java (Solution1.java) ------------------------------- |
| 32 | + |
| 33 | +import java.util.*; |
| 34 | + |
| 35 | +public class Solution1 { |
| 36 | + |
| 37 | + public static int findOpposite(int a, int b, int c) { |
| 38 | + int n = 2 * Math.abs(a - b); |
| 39 | + if (a > n || b > n || c > n) { |
| 40 | + return -1; |
| 41 | + } |
| 42 | + int half = n / 2; |
| 43 | + if (c + half <= n) { |
| 44 | + return c + half; |
| 45 | + } |
| 46 | + return c - half; |
| 47 | + } |
| 48 | + |
| 49 | + public static void main(String[] args) { |
| 50 | + Scanner sc = new Scanner(System.in); |
| 51 | + int t = sc.nextInt(); |
| 52 | + while (t-- > 0) { |
| 53 | + int a = sc.nextInt(); |
| 54 | + int b = sc.nextInt(); |
| 55 | + int c = sc.nextInt(); |
| 56 | + System.out.println(findOpposite(a, b, c)); |
| 57 | + } |
| 58 | + sc.close(); |
| 59 | + } |
| 60 | +} |
0 commit comments