Find the Maximum Achievable Number solution and explanation
9 more videos on this problem
2769. Find the Maximum Achievable Number | LEETCODE EASYcode Explainer · 4:17 · 4K views
Find the Maximum Achievable Number| LeetCode 2769 | CppCodeClips with Abhishek Ranjan · 2:08 · 1K views
Leetcode 2769 Find the Maximum Achievable NumberTech Diet · 2:42 · 1K views
2769. Find the Maximum Achievable Number (Leetcode Easy)Programming Live with Larry · 2:10 · 983 views
2769. Find the Maximum Achievable Number || Leetcode || Weekly Contest 353 || HindiAshish Kumar · 1:33 · 592 views
Double Your Coding Skills Python Challenge LeetCode 2769. Find the Maximum Achievable NumbervanAmsen · 4:09 · 554 views
6451. Find the Maximum Achievable Number | Leetcode Weekly Contest 353 | Solution Code.Optimization · 0:06 · 474 views
Leetcode Problem 2769. Find the Maximum Achievable Number. Leetcode Solution 2769 EASY. JavascriptCodingForBeginners · 4:05 · 458 views
#LeetCode 2769 | Find the Maximum Achievable Number #JavaSleepy Cracker · 1:45 · 108 views
my attempt at solving leetcode 2769 :find the maximum achievable number [SOLVED]Cameron Late · 5:22 · 56 views
The problem
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 decreasenumby1.
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
numby 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
numby 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).
def maxAchievableNumber(num, t):
return num + 2 * t
num = 4
t = 1
print(maxAchievableNumber(num, t))public class Solution {
public static int maxAchievableNumber(int num, int t) {
return num + 2 * t;
}
public static void main(String[] args) {
int num = 4, t = 1;
System.out.println(maxAchievableNumber(num, t));
}
}#include <iostream>
using namespace std;
int maxAchievableNumber(int num, int t) {
return num + 2 * t;
}
int main() {
int num = 3, t = 2;
cout << maxAchievableNumber(num, t) << endl;
return 0;
}function maxAchievableNumber(num, t) {
return num + 2 * t;
}
const num = 4, t = 1;
console.log(maxAchievableNumber(num, t));#include <stdio.h>
int maxAchievableNumber(int num, int t) {
return num + 2 * t;
}
int main() {
int num = 4, t = 1;
printf("%d\n", maxAchievableNumber(num, t));
return 0;
}using System;
class Solution {
public static int MaxAchievableNumber(int num, int t) {
return num + 2 * t;
}
static void Main(string[] args) {
int num = 3, t = 2;
Console.WriteLine(MaxAchievableNumber(num, t));
}
}In this Python implementation, the function 'maxAchievableNumber' adds double 't' to 'num'. This approach runs in constant time and is very straightforward to implement and understand.
Time Complexity: O(1)
Space Complexity: O(1)
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.
def maxAchievableNumber(num, t):
for _ in range(t):
num += 2
return num
num = 4
t = 1
print(maxAchievableNumber(num, t))public class Solution {
public static int maxAchievableNumber(int num, int t) {
while (t-- > 0) {
num += 2;
}
return num;
}
public static void main(String[] args) {
int num = 3, t = 2;
System.out.println(maxAchievableNumber(num, t));
}
}#include <iostream>
using namespace std;
int maxAchievableNumber(int num, int t) {
while (t-- > 0) {
num += 2;
}
return num;
}
int main() {
int num = 3, t = 2;
cout << maxAchievableNumber(num, t) << endl;
return 0;
}function maxAchievableNumber(num, t) {
for (let i = 0; i < t; i++) {
num += 2;
}
return num;
}
const num = 4, t = 1;
console.log(maxAchievableNumber(num, t));#include <stdio.h>
int maxAchievableNumber(int num, int t) {
for (int i = 0; i < t; ++i) {
num += 2;
}
return num;
}
int main() {
int num = 4, t = 1;
printf("%d\n", maxAchievableNumber(num, t));
return 0;
}using System;
class Solution {
public static int MaxAchievableNumber(int num, int t) {
while (t-- > 0) {
num += 2;
}
return num;
}
static void Main(string[] args) {
int num = 3, t = 2;
Console.WriteLine(MaxAchievableNumber(num, t));
}
}This Python solution uses a loop to continually add 2 to 'num', simulating the operations allowed. It matches the constraints specified by the problem.
Time Complexity: O(t)
Space Complexity: O(1)
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).
function theMaximumAchievableX(num: number, t: number): number {
return num + t * 2;
}func theMaximumAchievableX(num int, t int) int {
return num + t*2
}Complexity comparison
| Approach | Complexity |
|---|---|
| Mathematical Formula | Time Complexity: O(1) |
| Simulate Operations | Time Complexity: O(t) |
| Mathematics | – |
Complexity analysis
| 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 |
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.
