DEV Community

Alex Hunter
Alex Hunter

Posted on

2769. Find the Maximum Achievable Number: LeetCode Study Note from LeetCopilot

1. Problem Summary

  1. Task: Given integers num and t, a number x is achievable if you can apply at most t operations that change x by ±1 and simultaneously change num by ±1 so that after these operations x == num. Return the maximum achievable x.
  2. Category: Math / Invariants & Reachability.
  3. Difficulty: Easy.
  4. Key constraints impacting method: a. Each operation adjusts both numbers by 1 in some directions. b. The distance |x - num| can be reduced by at most 2 per operation (increase x while decreasing num, or vice versa). c. With at most t operations, the largest reachable x must satisfy x - num ≤ 2t.

2. Approaches & Strategies

  1. Distance (invariant) reasoning: Track the difference d = x - num. One operation can change d by -2, 0, or +2, so in at most t steps you can bridge any initial gap whose magnitude is ≤ 2t.
  2. Maximization logic: To maximize x, take the case x ≥ num. The largest x that can still meet num within t steps satisfies x - num = 2t, hence x = num + 2t.
  3. Why this is optimal and sufficient: a. Sufficiency: If x = num + 2t, perform t steps of x -= 1 and num += 1 to meet in the middle. b. Optimality: If x > num + 2t, even with t best-possible steps (shrinking distance by 2 each), the gap cannot close to 0.

3. My Solution & Code

  1. Strategy used: Convert the problem to a simple closed-form using the “distance shrinks by ≤2 per move” invariant; directly return num + 2 * t.
  2. Comparison to alternatives: Any brute-force simulation over operations or candidate x is unnecessary; the invariant yields an O(1) formula.
def maximumAchievableX(num: int, t: int) -> int:
    # Max x such that |x - num| <= 2t  ⇒ x_max = num + 2t
    return num + 2 * t
Enter fullscreen mode Exit fullscreen mode

4. Time & Space Complexity

  1. Time: O(1) due to the direct formula.
  2. Space: O(1) with no auxiliary storage.
  3. Trade-offs: A simulation approach would be O(t) and error-prone; the invariant gives the optimal constant-time solution.

5. Mistakes & Insights

  1. Misreading the operation: Thinking only one of x or num changes per step; in fact both change each time.
  2. Underestimating distance shrink: Each best move can reduce the gap by 2, not 1; missing this leads to num + t instead of num + 2t.
  3. Parity overthinking: Since both change by ±1 each step, parity mismatches are irrelevant; you can always meet in ≤ t steps if |x - num| ≤ 2t.
  4. Off-by-one errors: “At most t” includes using fewer than t steps; equality at exactly t steps is not required.

6. What’s Next

  1. Focus areas: a. Practice identifying invariants and meeting-in-the-middle style arguments. b. Convert operation-based problems into distance bounds or closed forms when possible.
  2. Related LeetCode problems: a. 1539. Kth Missing Positive Number (Easy) — Uses count/bounds reasoning to derive a direct answer without simulating all numbers. b. 2520. Count the Digits That Divide a Number (Easy) — Simple numerical reasoning, good for practicing careful constraints and invariants on digits. c. 2869. Minimum Operations to Collect Elements (Easy) — Reason about operations and minimal steps to reach a target condition, reinforcing operation-to-bound transformations.

I created this study note using LeetCopilot, an AI-powered chrome extension for LeetCode study and interview prep. LeetCopilot generate high-quality, personalized and structured study note for each problem, based on your learning history. Create you own study note with LeetCopilot today :)

Top comments (0)