Watch 10 video solutions for Find the Maximum Achievable Number, a easy level problem involving Math. This walkthrough by code Explainer has 3,839 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |