Skip to main content
Back to problems
Codeforces
Easy
Math
Greedy
Simulation
The Third Side

Given two side lengths, determine whether they can form a triangle with some positive third side, and if so provide a valid choice.

Acceptance 0%
Problem Statement

Problem

You are given two positive integers representing two side lengths of a triangle. Your task is to determine whether there exists a positive integer value for the third side so that the three sides can form a non-degenerate triangle.

A triangle is valid only if the sum of every pair of sides is strictly greater than the remaining side.

If such a third side exists, output any valid positive integer value for it. If no such value exists, output -1.

Notes

  • A non-degenerate triangle must satisfy all three strict triangle inequalities.
  • You do not need to maximize or minimize the third side unless needed to produce a valid answer.
  • Any valid integer third side is acceptable.

Input Format

  • The first line contains an integer t, the number of test cases.
  • Each of the next t lines contains two integers a and b.

Output Format

For each test case, print one integer:

  • a valid third side length, or
  • -1 if no valid triangle can be formed.

Constraints

  • 1 <= t <= $10^{4}$
  • 1 <= a, b <= $10^{9}$
  • The third side, if it exists, should be a positive integer.

Hints

  • A triangle needs the third side to be less than a + b and greater than |a-b|.
  • Since you only need any valid integer, look for a simple value that always works when possible.

Input Format

  • The first line contains t.
  • Each of the next t lines contains two integers a and b.

Output Format

For each test case, print one integer on a separate line: a valid third side or -1.

Constraints

  • 1 <= t <= $10^{4}$
  • 1 <= a, b <= $10^{9}$
  • Output must be a positive integer when a triangle is possible.
Examples
Sample cases returned by the problem API.

Example 1

Input

3
3 4
1 1
2 10

Output

2
1
-1

Explanation

For (3, 4), 2 works because 3 + 2 > 4, 4 + 2 > 3, and 3 + 4 > 2. For (1, 1), 1 forms an equilateral triangle. For (2, 10), no positive integer third side can satisfy 2 + x > 10 and 2 + 10 > x simultaneously with the remaining inequality.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.