Explanation
Since the number of people left of and the number of people right of must be equal, we can find the total number of people in the circle with .
From here, we can see if this circle is possible with ,
If any of these exceed the number of people in the circle, they can't have a partner.
Otherwise, we can subtract or add from to find the answer.
Implementation
Time Complexity:
C++
#include <iostream>using namespace std;int solve(long long a, long long b, long long c) {long long numGroup = abs(a - b) * 2;if (a > numGroup || b > numGroup || c > numGroup) {return -1;} else {long long ans = c + abs(a - b);
Java
import java.io.*;import java.util.*;public class WhosOpposite {static long solve(long a, long b, long c) {long numGroup = Math.abs(a - b) * 2;if (a > numGroup || b > numGroup || c > numGroup) {return -1;} else {
Python
def solve(a, b, c):num_group = abs(a - b) * 2if a > num_group or b > num_group or c > num_group:return -1else:ans = c + abs(a - b)"""If the answer is greater than the numberof people in the group, then we should
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!