Problem Solution

1 solutions

  • 0
    @ 2025-04-08 09:17:28

    Author : Kamonasish Roy
    Editorial :

    • You are given three integers: l, r, and x (with l ≤ r).
    • Your task is to find two integers p and q such that:
    • l ≤ p ≤ r

    • l ≤ q ≤ r

    • p - q = x

    • If there are multiple valid pairs, any one can be printed. If no valid pair exists, output "-1 -1".

    • Key Observations & Approach:

    • Case: x = 0

    When x is zero, the condition becomes p - q = 0, i.e., p = q.

    In this case, any number within [l, r] works.

    Solution: Choose p = l and q = l.

    • Case: x > 0

    We need p - q = x, so p must be greater than q.

    A natural choice is to let q be the smallest possible value (q = l).

    Then, set p = l + x.

    Validity Check: p must not exceed r, i.e., ensure l + x ≤ r.

    Solution: If l + x ≤ r, output p = l + x and q = l.

    • Case: x < 0

    Here, p - q = x (a negative number) implies that p < q.

    A convenient choice is to let q be the largest possible value (q = r).

    Then, set p = r + x (since x is negative, p will be less than r).

    Validity Check: p must be at least l, i.e., ensure r + x ≥ l.

    • Solution: If r + x ≥ l, output p = r + x and q = r.

    • No Valid Pair:

    If neither of the above conditions (for x > 0 or x < 0) are satisfied, then no valid pair (p, q) exists.

    Solution: Output "-1 -1".

    • Example:

    For l = 5, r = 10, x = 3 (x > 0):

    Set q = 5 and p = 5 + 3 = 8.

    Check: 8 ≤ 10 is true, so output: 8 5.

    For l = 5, r = 10, x = -3 (x < 0):

    Set q = 10 and p = 10 - 3 = 7.

    Check: 7 ≥ 5 is true, so output: 7 10.

    Code (Python):

    # Author: Kamonasish Roy (Bullet)
    # Time: 2025-03-24 04:20:12
    
    def solve():
        t = int(input())  # Number of test cases
        for _ in range(t):
            l, r, x = map(int, input().split())
            
            if abs(x) > (r - l):
                print(-1, -1)
                continue
            
            p = l
            q = l + abs(x)
            
            if x > 0:
                p, q = q, p
            
            print(p, q)
    
    # Call the function to solve the problem
    solve()
    
    
  • 1

Information

ID
1186
Difficulty
1
Category
Math | Brute_Force Click to Show
Tags
# Submissions
109
Accepted
44
Accepted Ratio
40%
Uploaded By