2769. Find the Maximum Achievable Number
Problem Description
You are given two integers: num
and t
.
A number x
is considered achievable if it can become equal to num
after applying the following operation at most t
times:
- Increase or decrease
x
by1
, and simultaneously increase or decreasenum
by1
.
Your task is to find the maximum possible value of x
that is achievable.
The key insight is understanding how the operation works: when you change x
and num
simultaneously, you're affecting the gap between them. If you decrease x
by 1
and increase num
by 1
, the distance between them decreases by 2
. Conversely, if you increase x
by 1
and decrease num
by 1
, the distance increases by 2
.
To maximize x
, you want to start with x
as large as possible while still being able to make it equal to num
within t
operations. Since each operation can reduce the gap by 2
, and you have t
operations available, the maximum initial gap can be 2 * t
. Therefore, the maximum achievable x
is num + 2 * t
.
Intuition
Let's think about what happens during each operation. When we apply the operation, we can either:
- Decrease
x
by1
and increasenum
by1
, or - Increase
x
by1
and decreasenum
by1
The crucial observation is that in both cases, the distance between x
and num
changes by exactly 2
. In the first case, the gap decreases by 2
, and in the second case, it increases by 2
.
Since we want to find the maximum possible x
, we should start with x
as large as possible. But how large can it be? Well, we need to ensure that after at most t
operations, x
equals num
.
If we start with a large x
(larger than num
), we need to bring it down to meet num
. The most efficient way is to decrease x
while increasing num
in each operation, which closes the gap by 2
per operation.
With t
operations available, we can close a gap of at most 2 * t
. Therefore, the maximum starting value of x
would be num + 2 * t
.
To verify: Starting with x = num + 2 * t
, after t
operations of decreasing x
by 1
and increasing num
by 1
:
x
becomes:(num + 2 * t) - t = num + t
num
becomes:num + t
- They meet at
num + t
, confirming our answer.
This explains why the solution is simply num + 2 * t
.
Learn more about Math patterns.
Solution Approach
The solution is remarkably straightforward once we understand the mathematical relationship between the operations and the achievable values.
From our analysis, we know that:
- Each operation can change the distance between
x
andnum
by exactly2
- To maximize
x
, we want to start with the largest possible value - With
t
operations, we can bridge a maximum gap of2 * t
Therefore, the maximum achievable x
is simply num + 2 * t
.
The implementation is a direct translation of this mathematical formula:
class Solution:
def theMaximumAchievableX(self, num: int, t: int) -> int:
return num + t * 2
This is a constant time O(1)
solution with O(1)
space complexity. No loops, no complex data structures, just a simple mathematical calculation.
The elegance of this solution lies in recognizing that we don't need to simulate the operations or try different starting values. The mathematical insight that each operation affects the gap by 2
immediately gives us the answer: start x
at num + 2 * t
, and use all t
operations to decrease x
while increasing num
, meeting in the middle at num + t
.
Ready to land your dream job?
Unlock your dream job with a 5-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's walk through a concrete example with num = 4
and t = 3
.
We want to find the maximum possible value of x
that can become equal to num
after at most 3 operations.
Step 1: Calculate the maximum achievable x
Using our formula: x = num + 2 * t = 4 + 2 * 3 = 10
Step 2: Verify this works
Let's start with x = 10
and num = 4
, and show how they can meet:
- Initial state:
x = 10
,num = 4
(gap = 6) - Operation 1: Decrease
x
by 1 (→ 9), increasenum
by 1 (→ 5), gap = 4 - Operation 2: Decrease
x
by 1 (→ 8), increasenum
by 1 (→ 6), gap = 2 - Operation 3: Decrease
x
by 1 (→ 7), increasenum
by 1 (→ 7), gap = 0
After exactly 3 operations, both values meet at 7. Notice that:
x
went from 10 to 7 (decreased by 3)num
went from 4 to 7 (increased by 3)- The initial gap of 6 was closed by 2 per operation (6 → 4 → 2 → 0)
Step 3: Confirm no larger x is possible
Could we start with x = 11
instead? Let's check:
- Initial gap would be 11 - 4 = 7
- With 3 operations, we can close a gap of at most 2 * 3 = 6
- Since 7 > 6, we cannot make
x = 11
equal tonum = 4
within 3 operations
This confirms that num + 2 * t = 10
is indeed the maximum achievable value of x
.
Solution Implementation
1class Solution:
2 def theMaximumAchievableX(self, num: int, t: int) -> int:
3 """
4 Calculate the maximum achievable value of x.
5
6 In each operation, we can either:
7 - Increase num by 1 and decrease x by 1, OR
8 - Decrease num by 1 and increase x by 1
9
10 After t operations, the maximum x occurs when we always increase num,
11 which means x can be at most num + 2*t (since each operation increases
12 the gap between num and x by 2).
13
14 Args:
15 num: The starting integer value
16 t: The number of operations allowed
17
18 Returns:
19 The maximum achievable value of x after t operations
20 """
21 # Each operation can increase the difference between num and x by 2
22 # So the maximum achievable x is num plus twice the number of operations
23 return num + t * 2
24
1class Solution {
2 /**
3 * Calculates the maximum achievable value of X.
4 *
5 * The maximum achievable X is obtained by performing t operations,
6 * where each operation can either:
7 * - Increase num by 1 and decrease X by 1, or
8 * - Decrease num by 1 and increase X by 1
9 *
10 * To maximize X, we should always decrease num and increase X.
11 * After t operations, num becomes (num - t) and X becomes (X + t).
12 * For them to be equal: num - t = X + t
13 * Therefore: X = num - 2t
14 * But since we want the maximum X that can equal num after operations,
15 * we need to find when X - t = num + t (X decreases, num increases)
16 * Therefore: X = num + 2t
17 *
18 * @param num The initial number value
19 * @param t The number of operations allowed
20 * @return The maximum achievable value of X
21 */
22 public int theMaximumAchievableX(int num, int t) {
23 // Maximum X is achieved by adding twice the number of operations to num
24 return num + t * 2;
25 }
26}
27
1class Solution {
2public:
3 /**
4 * Calculates the maximum achievable value of x.
5 *
6 * The maximum achievable x is found by considering that in each operation:
7 * - We can increase num by 1 and decrease x by 1, OR
8 * - We can decrease num by 1 and increase x by 1
9 *
10 * To maximize x, we should always choose to increase x (second option).
11 * After t operations, num will be (num - t) and x will be (x + t).
12 * For them to be equal: num - t = x + t
13 * Therefore: x = num - 2t
14 *
15 * But we want the maximum achievable x that can equal num after operations.
16 * Starting from x, after t operations where we decrease x each time,
17 * x becomes (x - t) and num becomes (num + t).
18 * For equality: x - t = num + t
19 * Therefore: x = num + 2t
20 *
21 * @param num The initial number value
22 * @param t The number of operations allowed
23 * @return The maximum achievable value of x
24 */
25 int theMaximumAchievableX(int num, int t) {
26 // Maximum achievable x is the initial number plus twice the operations count
27 return num + 2 * t;
28 }
29};
30
1/**
2 * Calculates the maximum achievable value of x
3 *
4 * The maximum achievable value is obtained by performing t operations,
5 * where each operation can either increment or decrement both num and x by 1.
6 * To maximize x, we should always increment x and decrement num in each operation.
7 * This gives us a net increase of 2 for x per operation.
8 *
9 * @param num - The initial number to start with
10 * @param t - The number of operations that can be performed
11 * @returns The maximum achievable value of x after t operations
12 */
13function theMaximumAchievableX(num: number, t: number): number {
14 // Each operation can increase the gap between x and num by 2
15 // Starting from x = num, we can achieve x = num + 2*t
16 return num + t * 2;
17}
18
Time and Space Complexity
The time complexity is O(1)
because the function performs a fixed number of operations regardless of the input size. It simply executes one multiplication (t * 2
) and one addition (num + t * 2
), which are constant-time arithmetic operations.
The space complexity is O(1)
because the function only uses a constant amount of extra memory. No additional data structures are created, and the space used does not depend on the input values. The function directly returns the calculated result without storing any intermediate values in variables.
Learn more about how to find time and space complexity quickly.
Common Pitfalls
1. Misunderstanding the Operation Direction
Pitfall: Many people initially think that to maximize x
, they should always increase both x
and num
simultaneously, leading to the incorrect formula x = num + t
.
Why it happens: The problem statement says we can "increase or decrease x by 1, and simultaneously increase or decrease num by 1", which might be misread as both values moving in the same direction.
Correct Understanding: The operations are:
- Increase
x
by 1 AND decreasenum
by 1 (or vice versa) - The values move in opposite directions, not the same direction
Solution: Remember that each operation changes the gap between x
and num
by 2, not by 0. If both moved in the same direction, the gap would remain constant.
2. Overthinking with Complex Algorithms
Pitfall: Attempting to use binary search, dynamic programming, or greedy algorithms to find the maximum x
.
Why it happens: The problem appears complex at first glance, and the mention of "maximum" often triggers thoughts of optimization algorithms.
Example of overthinking:
# Unnecessary complex approach
def theMaximumAchievableX(self, num: int, t: int) -> int:
left, right = num - t * 2, num + t * 2
while left < right:
mid = (left + right + 1) // 2
if self.canReach(mid, num, t):
left = mid
else:
right = mid - 1
return left
Solution: Recognize that this is a pure mathematical problem. Once you understand that each operation affects the distance by 2, the answer becomes a simple formula: num + 2 * t
.
3. Integer Overflow Concerns (Language-Specific)
Pitfall: In languages with fixed integer sizes (like C++ or Java), computing num + 2 * t
might cause overflow if the values are large.
Solution: In Python, this isn't an issue due to arbitrary precision integers. In other languages, consider using appropriate data types:
// C++ solution with overflow consideration
long long theMaximumAchievableX(int num, int t) {
return static_cast<long long>(num) + 2LL * t;
}
4. Off-by-One Errors in Understanding
Pitfall: Confusing whether we need exactly t
operations or at most t
operations, potentially leading to formulas like num + 2 * (t - 1)
or num + 2 * (t + 1)
.
Why it happens: Some problems require exact operation counts, while others allow "up to" a certain number.
Solution: The problem clearly states "at most t times", meaning we can use anywhere from 0 to t operations. To maximize x
, we use all t operations, giving us num + 2 * t
.
Depth first search is equivalent to which of the tree traversal order?
Recommended Readings
Math for Technical Interviews How much math do I need to know for technical interviews The short answer is about high school level math Computer science is often associated with math and some universities even place their computer science department under the math faculty However the reality is that you
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https assets algo monster recursion jpg You first call Ben and ask
Want a Structured Path to Master System Design Too? Don’t Miss This!