LeetCode 2769 - Find the Maximum Achievable Number
The problem gives us two integers, num and t. We are allowed to perform a specific operation at most t times. In one operation, we must simultaneously modify both numbers involved: - Increase or decrease x by 1 - At the same time, increase or decrease num by 1 The goal is to…
Difficulty: 🟢 Easy
Topics: Math
Solution
Problem Understanding
The problem gives us two integers, num and t. We are allowed to perform a specific operation at most t times. In one operation, we must simultaneously modify both numbers involved:
- Increase or decrease
xby1 - At the same time, increase or decrease
numby1
The goal is to determine the maximum possible starting value of x such that, after performing at most t operations, x can become equal to num.
The important detail is that both values change simultaneously during every operation. This means the distance between x and num changes by 2 per operation when we move them toward each other.
Suppose we start with some value x that is larger than num. To make them equal in one operation:
- Decrease
xby1 - Increase
numby1
This reduces the difference between them by 2.
If we can perform this operation at most t times, then the largest achievable difference between the initial x and num is 2 * t.
Therefore, the maximum achievable value of x is:
$x = num + 2t$
The constraints are very small:
1 <= num, t <= 50
This tells us that performance is not a concern. Even a brute-force solution would work comfortably within the limits. However, the problem has a direct mathematical observation that allows us to solve it instantly.
An important edge case is when t = 0. In that situation, no operations are allowed, so the only achievable value is num itself. Another edge case is the smallest possible inputs, such as num = 1 and t = 1, where the formula still works correctly. Since all values are positive and very small, we do not need to worry about overflow or invalid input.
Approaches
Brute Force Approach
A brute-force solution would try all possible candidate values of x and determine whether each one can become equal to num within t operations.
For a candidate value x greater than num, the difference is:
$x - num$
Each operation reduces this difference by 2, because x decreases by 1 while num increases by 1.
So a candidate x is achievable if:
$x - num \leq 2t$
We could iterate through possible values of x, check this condition, and keep track of the maximum valid value.
This approach is correct because it explicitly verifies every possible candidate. However, it is unnecessary because the mathematical relationship is straightforward.
Optimal Mathematical Approach
The key observation is that every operation can increase the achievable starting distance between x and num by exactly 2.
If we perform the operation t times:
xcan effectively starttlarger because it decreases each timenumcan effectively movetupward because it increases each time
Together, this creates a maximum gap of:
$2t$
Therefore, the maximum achievable number is simply:
$num + 2t$
This gives us a constant time solution.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(t) | O(1) | Checks possible values of x one by one |
| Optimal | O(1) | O(1) | Uses direct mathematical observation |
Algorithm Walkthrough
- Read the input values
numandt. - Observe that one operation changes the gap between
xandnumby2. - Since we can perform at most
toperations, the maximum additional distance we can create is2 * t. - Add this value to
numto compute the maximum achievablex. - Return the result.
Why it works
The algorithm works because each operation simultaneously moves x and num toward each other by one unit each. This reduces their difference by exactly 2. Therefore, after t operations, the largest possible initial gap between x and num is 2t. Adding this maximum gap to num gives the largest achievable starting value of x.
Python Solution
class Solution:
def theMaximumAchievableX(self, num: int, t: int) -> int:
return num + 2 * t
The implementation directly applies the mathematical formula derived earlier.
The expression 2 * t computes the maximum possible gap that can be eliminated through the allowed operations. Adding this value to num produces the largest achievable starting value of x.
Since the problem only requires returning the computed result, no loops, conditionals, or additional data structures are necessary.
Go Solution
func theMaximumAchievableX(num int, t int) int {
return num + 2*t
}
The Go implementation is identical in logic to the Python version.
Go uses explicit integer arithmetic, but there are no overflow concerns because the constraints are extremely small. No slices, maps, or additional memory allocations are needed.
Worked Examples
Example 1
Input:
num = 4
t = 1
We apply the formula:
$4 + 2(1) = 6$
| Step | Value |
|---|---|
Initial num |
4 |
| Operations allowed | 1 |
| Maximum extra distance | 2 |
| Result | 6 |
To verify:
-
Start with
x = 6 -
Perform one operation:
-
Decrease
xto5 -
Increase
numto5
Now they are equal.
Example 2
Input:
num = 3
t = 2
We apply the formula:
$3 + 2(2) = 7$
| Step | Value |
|---|---|
Initial num |
3 |
| Operations allowed | 2 |
| Maximum extra distance | 4 |
| Result | 7 |
Verification:
-
Start with
x = 7 -
Operation 1:
-
x = 6 -
num = 4 -
Operation 2:
-
x = 5 -
num = 5
The values become equal after two operations.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(1) | Only one arithmetic computation is performed |
| Space | O(1) | No extra memory is used |
The solution performs a constant number of operations regardless of the input size. Since no additional data structures are allocated, the memory usage also remains constant.
Test Cases
solution = Solution()
assert solution.theMaximumAchievableX(4, 1) == 6 # Example 1
assert solution.theMaximumAchievableX(3, 2) == 7 # Example 2
assert solution.theMaximumAchievableX(1, 1) == 3 # Smallest num with one operation
assert solution.theMaximumAchievableX(1, 0) == 1 # No operations allowed
assert solution.theMaximumAchievableX(50, 50) == 150 # Maximum constraints
assert solution.theMaximumAchievableX(10, 5) == 20 # Typical middle-range values
assert solution.theMaximumAchievableX(7, 3) == 13 # Multiple operations
assert solution.theMaximumAchievableX(25, 0) == 25 # t = 0 edge case
| Test | Why |
|---|---|
num=4, t=1 |
Validates the first example |
num=3, t=2 |
Validates the second example |
num=1, t=1 |
Tests smallest positive input |
num=1, t=0 |
Ensures no-operation case works |
num=50, t=50 |
Tests upper constraint boundary |
num=10, t=5 |
Verifies standard input behavior |
num=7, t=3 |
Confirms multiple operations calculation |
num=25, t=0 |
Another no-operation validation |
Edge Cases
One important edge case is when t = 0. In this scenario, no operations can be performed, so x must already equal num. A buggy implementation might still add extra value unnecessarily. The formula handles this correctly because num + 2 * 0 equals num.
Another important edge case is the smallest valid input values. For example, num = 1 and t = 1. Small inputs sometimes expose off-by-one mistakes in mathematical reasoning. The implementation correctly computes 1 + 2 = 3.
A third edge case is the maximum constraint values, such as num = 50 and t = 50. Even though the numbers are still small, this case confirms that the formula scales correctly to the upper bounds. The result becomes 150, which is computed safely and instantly.
A subtle edge case involves understanding the operation itself. Since both numbers change simultaneously, the distance changes by 2, not 1. A common mistake is returning num + t instead of num + 2 * t. The implementation avoids this error by directly modeling the true effect of each operation.