Given two integers, num and t. A number is achievable if it can become equal to num after applying the following operation:
1, and simultaneously increase or decrease num by 1.Return the maximum achievable number after applying the operation at most t times.
Example 1:
Input: num = 4, t = 1
Output: 6
Explanation:
Apply the following operation once to make the maximum achievable number equal to num:
num by 1.Example 2:
Input: num = 3, t = 2
Output: 7
Explanation:
Apply the following operation twice to make the maximum achievable number equal to num:
num by 1.
Constraints:
1 <= num, t <= 50Problem Overview: You receive two integers num and t. Starting with a value equal to num, you can perform up to t operations that effectively move the value away from num. The goal is to compute the largest value that can still be considered achievable from num after those operations.
Approach 1: Simulate Operations (O(t) time, O(1) space)
The most direct way to reason about the problem is to simulate the operations. Each operation lets you increase the candidate value by 1 while decreasing num by 1. The distance between them therefore grows by 2 per operation. If you repeat this process t times using a loop, the achievable value increases by 2 on every step. Start with result = num and run a loop t times adding 2 each iteration. The final value becomes num + 2 * t. This approach demonstrates the mechanics clearly and works well when explaining the idea during an interview. Time complexity is O(t) because you iterate once per operation, and space complexity is O(1) since only a few variables are used.
Approach 2: Mathematical Formula (O(1) time, O(1) space)
Once you observe the pattern from simulation, the process collapses into a simple mathematical expression. Every operation expands the achievable distance by 2. After t operations, the maximum reachable value becomes num + 2 × t. Instead of looping, compute this directly and return the result. This eliminates iteration entirely and runs in constant time. The algorithm uses basic arithmetic from math and avoids unnecessary computation. Time complexity is O(1) and space complexity is also O(1).
The key insight is recognizing that each operation simultaneously moves the two values in opposite directions, doubling the net change. That observation converts what looks like a step-by-step process into a single formula.
Recommended for interviews: Start by describing the simulation to show you understand how the operations affect the numbers. Then derive the formula num + 2 * t. Interviewers usually expect the constant-time mathematical solution since the problem belongs to the Math category, but explaining the simulated reasoning demonstrates problem-solving clarity. If asked to code quickly, go directly with the formula.
The maximum achievable number is derived directly using a mathematical formula: add '2t' to 'num'. This works because for each operation, you can increase 'num' by 2 units (either by incrementing once then another by modifying the 'virtual' number).
This solution defines a function 'maxAchievableNumber' that directly calculates the maximum achievable number by adding twice the value of 't' to 'num'. This operation is computationally efficient and executes in constant time.
Time Complexity: O(1)
Space Complexity: O(1)
Simulate the operation by iteratively updating 'num' using a loop, increasing it twice per iteration. This method helps in understanding the step-by-step changes in 'num' and in verifying the mathematical approach.
This C solution iteratively updates 'num' by adding 2 in each iteration for 't' iterations. It mimics the operation to ensure the maximum achievable number.
Time Complexity: O(t)
Space Complexity: O(1)
| Approach | Complexity |
|---|---|
| Mathematical Formula | Time Complexity: O(1) |
| Simulate Operations | Time Complexity: O(t) |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulate Operations | O(t) | O(1) | When explaining the mechanics of the operations step-by-step |
| Mathematical Formula (num + 2*t) | O(1) | O(1) | Preferred solution for interviews and production due to constant time |
2769. Find the Maximum Achievable Number | LEETCODE EASY • code Explainer • 3,839 views views
Watch 9 more video solutions →Practice Find the Maximum Achievable Number with our built-in code editor and test cases.
Practice on FleetCode