Table of Contents

ExplanationImplementation

Official Editorial (C++)

Explanation

Since the number of people left of aa and the number of people right of bb must be equal, we can find the total number of people in the circle with ab2|a - b| \cdot 2.

From here, we can see if this circle is possible with a,b,c>numGroupa, b, c > \texttt{numGroup},

If any of these exceed the number of people in the circle, they can't have a partner.

Otherwise, we can subtract or add ab|a - b| from cc to find the answer.

Implementation

Time Complexity: O(1)\mathcal{O}(1)

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) * 2
if a > num_group or b > num_group or c > num_group:
return -1
else:
ans = c + abs(a - b)
"""
If the answer is greater than the number
of 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!