Find the Maximum Achievable Number - Problem
You're given two integers: num and t. Your goal is to find the maximum achievable number x that can become equal to num after applying a special operation at most t times.
The operation is unique: you can increase or decrease both x and num by 1 simultaneously. This means in a single operation, you can:
- Increase both
xandnumby 1, OR - Decrease both
xandnumby 1
The key insight is that since both numbers change together, you're effectively controlling the gap between them. Return the maximum possible starting value of x.
Example: If num = 4 and t = 1, you could start with x = 6. Then decrease both by 1: x becomes 5, num becomes 3. They're still 2 apart, but we've used our operation.
Input & Output
example_1.py โ Basic Case
$
Input:
num = 4, t = 1
โบ
Output:
6
๐ก Note:
Start with x = 6. After 1 operation of decreasing both by 1: x becomes 5, num becomes 3. We can't make them equal, but if we start with x = 6 and increase both by 1: x becomes 7, num becomes 5. Still not equal. Actually, we need x = 4 + 2*1 = 6. Starting with x = 6, we decrease both by 1 twice? No, we only have 1 operation. The maximum achievable x is num + 2*t = 4 + 2*1 = 6.
example_2.py โ Multiple Operations
$
Input:
num = 3, t = 2
โบ
Output:
7
๐ก Note:
With 2 operations available, we can start with x = 7. The gap between x and num is 4, which equals 2*t. We can close this gap by decreasing both numbers twice, making x = 5 and num = 1, or by using operations strategically to make them equal.
example_3.py โ No Operations
$
Input:
num = 10, t = 0
โบ
Output:
10
๐ก Note:
With no operations allowed, x must already equal num, so the maximum achievable x is 10.
Constraints
- 1 โค t โค 50
- -109 โค num โค 109
- Both num and t are integers
Visualization
Tap to expand
Understanding the Visualization
1
Maximum Starting Position
Start x as far right as possible while still reachable
2
Close the Gap
Each operation can close the gap by 2 (move toward each other)
3
Perfect Meeting
After t operations, they meet at the same position
Key Takeaway
๐ฏ Key Insight: The maximum achievable x is num + 2*t because each operation can close the gap by at most 2, so we need at most 2*t initial gap.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code