Find the Maximum Achievable Number - Problem

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

Operation: Increase or decrease x by 1, and simultaneously increase or decrease num by 1.

Return the maximum possible value of x.

Input & Output

Example 1 — Basic Case
$ Input: num = 4, t = 1
Output: 6
💡 Note: Start with x = 6. Operation: x decreases to 5, num increases to 5. Now x = num = 5. Maximum achievable x was 6.
Example 2 — Multiple Operations
$ Input: num = 3, t = 2
Output: 7
💡 Note: Start with x = 7. After 2 operations moving optimally: x can decrease to 5, num can increase to 5. Maximum achievable x was 7.
Example 3 — Zero Operations
$ Input: num = 5, t = 0
Output: 5
💡 Note: No operations allowed, so x must equal num = 5 from the start. Maximum x is 5.

Constraints

  • 1 ≤ num ≤ 1000
  • 1 ≤ t ≤ 1000

Visualization

Tap to expand
Maximum Achievable Number INPUT 2 3 4 5 6 4 num num = 4 Starting value t = 1 Operations Operation Rules: x can +1 or -1 num can +1 or -1 Both change together At most t times ALGORITHM STEPS 1 Understand Goal Maximize x where x can become equal to num 2 Key Observation Each op: x-1 and num+1 closes gap by 2 3 Derive Formula Max gap = 2*t x = num + 2*t 4 Calculate x = 4 + 2*1 = 6 num + 2*t 4 + 2*1 = 6 O(1) time complexity FINAL RESULT 3 4 5 6 7 4 num 6 x gap = 2 Verification (t=1 op): x=6: x-1=5, num+1=5 Both become 5 - OK x equals num after 1 op OUTPUT 6 Key Insight: To maximize x, we want x as far from num as possible while still being achievable. Each operation closes the gap by 2 (x decreases by 1, num increases by 1). With t operations, we can close a gap of 2t. Therefore, maximum achievable x = num + 2*t = 4 + 2 = 6. TutorialsPoint - Find the Maximum Achievable Number | Optimal O(1) Solution
Asked in
Google 15 Amazon 12 Microsoft 8
15.0K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen