Skip to main content

Find the Maximum Achievable Number - Solution & Explanation

EasyMath10 min readAsked at: Amazon, Microsoft, Google +1
Practice this problem

Problem Statement

Given two integers, num and t. A number is achievable if it can become equal to num after applying the following operation:

  • Increase or decrease the number by 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:

  • Decrease the maximum achievable number by 1, and increase 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:

  • Decrease the maximum achievable number by 1, and increase num by 1.

 

Constraints:

  • 1 <= num, t <= 50

Approach Overview

Problem 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 1: Mathematical 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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1)
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Simulate Operations

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(t)
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: 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.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Mathematical Formula

Time Complexity: O(1)
Space Complexity: O(1)

Simulate Operations

Time Complexity: O(t)
Space Complexity: O(1)

Mathematics—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simulate OperationsO(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

Video Solution

2769. Find the Maximum Achievable Number | LEETCODE EASY • code Explainer • 4,057 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find the Maximum Achievable Number easy or hard?
Find the Maximum Achievable Number is classified as an Easy problem with a very high acceptance rate (over 90%). The challenge mainly tests whether you recognize the mathematical pattern instead of simulating every operation.
Find the Maximum Achievable Number Python/Java solution
The implementation in Python, Java, C++, or JavaScript is a single line: return num + 2 * t. Because the algorithm is constant time, the code remains identical across most languages.
How to solve Find the Maximum Achievable Number in O(1)?
Observe that each operation increases the distance between the candidate value and num by 2. After t operations, the maximum achievable number becomes num + 2 * t. Computing this expression directly gives an O(1) solution.
What is the best approach for Find the Maximum Achievable Number?
The optimal approach uses a direct mathematical formula: result = num + 2 * t. Each operation effectively increases the achievable value by 2. This removes the need for simulation and runs in O(1) time with O(1) space.
Is Find the Maximum Achievable Number asked at Google/Amazon/Meta?
This problem is categorized as an Easy math problem and is more commonly used for screening basic reasoning rather than advanced interviews. Similar arithmetic and pattern-recognition questions appear in early interview rounds at large tech companies.
What data structure is used in Find the Maximum Achievable Number?
No data structure is required. The solution relies purely on arithmetic reasoning and a simple mathematical formula derived from the effect of each operation.
What is the time complexity of Find the Maximum Achievable Number?
The optimal solution runs in O(1) time because it calculates the answer using a constant-time formula. A simulation approach would take O(t) time since it repeats the operation t times.

Ready to solve this problem?

Practice Find the Maximum Achievable Number with our built-in code editor and test cases.

Practice on FleetCode