LeetCode Problem Workspace

Find the Maximum Achievable Number

Determine the largest number achievable by repeatedly applying a simple math operation up to t times efficiently.

category

1

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Math-driven solution strategy

bolt

Answer-first summary

Determine the largest number achievable by repeatedly applying a simple math operation up to t times efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math-driven solution strategy

Try AiBox Copilotarrow_forward

The maximum achievable number can be calculated by leveraging a straightforward addition strategy derived from the given num and t. By incrementally applying the operation up to t times, you ensure each step increases the result optimally. This approach avoids overcomplication and guarantees reaching the largest possible number within the constraints.

Problem Statement

Given two integers num and t, compute the maximum number x that can be formed by performing the operation: increase x by 2, up to t times. Each operation should optimally push x toward its maximum achievable value.

Return the largest value of x that satisfies these operations. For example, if num = 4 and t = 1, the maximum achievable number is 6 after one operation. Constraints ensure 1 <= num, t <= 50.

Examples

Example 1

Input: num = 4, t = 1

Output: 6

Apply the following operation once to make the maximum achievable number equal to num :

Example 2

Input: num = 3, t = 2

Output: 7

Apply the following operation twice to make the maximum achievable number equal to num :

Constraints

  • 1 <= num, t <= 50

Solution Approach

Direct Math Calculation

Start with num and iteratively apply the operation that adds 2, repeating t times. The sum num + 2*t yields the maximum achievable number without extra iterations.

Simulation Approach

Simulate each operation step-by-step, adding 2 per operation until t operations are completed. This matches the mathematical approach but helps visualize the incremental changes.

Optimal Formula Application

Use the formula x = num + 2*t directly to compute the result in constant time. This leverages the math-driven pattern and ensures no unnecessary loops or checks.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(1) when using the formula approach, O(t) for simulation. Space complexity is O(1) in all cases since only a few variables are tracked.

What Interviewers Usually Probe

  • Look for immediate recognition of the addition pattern in the operation.
  • Check if candidate correctly computes maximum without redundant iterations.
  • Verify awareness of constant-time formula versus loop simulation.

Common Pitfalls or Variants

Common pitfalls

  • Overcomplicating with unnecessary loops or conditional checks.
  • Misunderstanding the operation effect leading to incorrect incremental addition.
  • Failing to consider the full t operations to reach maximum.

Follow-up variants

  • Change the operation to multiplication instead of addition, requiring a different formula approach.
  • Allow t to vary dynamically based on input conditions.
  • Introduce a negative operation, challenging candidate to maximize under alternating operations.

FAQ

What is the maximum achievable number for num = 4, t = 1?

Applying the operation once gives 4 + 2*1 = 6, which is the maximum achievable number.

How does the Math-driven solution pattern apply here?

It allows directly computing x = num + 2*t instead of iterating, saving time and avoiding unnecessary steps.

Can this problem be solved with a loop?

Yes, simulating each operation step-by-step works but is less efficient than using the direct formula.

What if t is larger than num?

The formula still applies; x = num + 2*t maximizes the achievable number regardless of relative sizes.

Are there variations in the operation for this problem?

Yes, operations like multiplication or alternating increases can create different variants requiring adjusted approaches.

terminal

Solution

Solution 1: Mathematics

Notice that every time we can decrease $x$ by $1$ and increase $num$ by $1$, the difference between $x$ and $num$ will decrease by $2$, and we can do this operation at most $t$ times, so the maximum reachable number is $num + t \times 2$.

1
2
3
class Solution:
    def theMaximumAchievableX(self, num: int, t: int) -> int:
        return num + t * 2
Find the Maximum Achievable Number Solution: Math-driven solution strategy | LeetCode #2769 Easy