Find the Maximum Achievable Number

Given two integers, `num` and `t`. A number `x` is **achievable** if it can become equal to `num` after applying the following operation at most `t` times: - Increase or decrease `x` by `1`, and **simultaneously** increase or decrease `num` by `1`. Return the maximum possible value of `x`.

30:00

Find the Maximum Achievable Number
easy
Free written solution
Topics
Companies

Given two integers, num and t. A number x is achievable if it can become equal to num after applying the following operation at most t times:

  • Increase or decrease x by 1, and simultaneously increase or decrease num by 1.

Return the maximum possible value of x.

Example 1:
Input: {"num":4,"t":1}
Output: 6
Constraints:
  • 1≤num,t≤501 \leq num, t \leq 50

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"num":4,"t":1}

Each operation can move x one step and num one step at the same time. Moving them toward each other shrinks the distance by 2, so t = 1 operations close a distance of at most 2.

number line

3

4

5

6

7

num 4

distance ?

ops used 0 / 1

each op: x - 1 and num + 1, distance - 2

Variables
VariableValue
num4
t1
DepthFunction Call
Stack empty
0/3