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 x and num by 1, OR
  • Decrease both x and num by 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
The Gap Closing StrategyNumber Linenumx (start)gap = 2tAfter Operation 1gap = 2(t-1)After t Operationsx = numgap = 0Key Insight: Maximum x = num + 2tEach operation can reduce the gap by exactly 2
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.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
8.9K Views
Medium Frequency
~5 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen