Table of Contents

ExplanationImplementation

Official Editorial (C++)

Explanation

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

This problem asks us to find the number that should be added to another number so that it is strictly greater than 2 other numbers. In other words, letting a,b,ca,b,c be the values of the 33 given numbers and nn be the number that will be added, we get the following equation:

a+n=max(b,c)+1a+n=\max(b,c)+1

Solving this, we get n=max(b,x)+1an=max(b,x)+1-a. With this, we can calculate nn for each of aa, bb, and cc. However, it is important to note that if n<0n<0, then we should output 00, since the value to add can't be negative.

Implementation

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int tc = 0; tc < t; tc++) {
int a, b, c;
cin >> a >> b >> c;

Java

import java.io.*;
import java.util.*;
public class Election {
public static void main(String[] args) throws IOException {
Kattio io = new Kattio();
int t = io.nextInt();
for (int tc = 0; tc < t; tc++) {
int a = io.nextInt();
int b = io.nextInt();

Python

for _ in range(int(input())):
a, b, c = map(int, input().split())
maxa = max(b, c)
maxb = max(a, c)
maxc = max(a, b)
print(max(0, maxa + 1 - a), max(0, maxb + 1 - b), max(0, maxc + 1 - c))

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!