Add Two Integers - Problem

Given two integers num1 and num2, return the sum of the two integers.

Constraints:

  • -100 ≤ num1, num2 ≤ 100

Input & Output

Example 1 — Basic Addition
$ Input: num1 = 12, num2 = 5
Output: 17
💡 Note: Simply add the two integers: 12 + 5 = 17
Example 2 — Negative Numbers
$ Input: num1 = -10, num2 = 4
Output: -6
💡 Note: Adding a positive to a negative: -10 + 4 = -6
Example 3 — Both Negative
$ Input: num1 = -1, num2 = -1
Output: -2
💡 Note: Adding two negatives: -1 + (-1) = -2

Constraints

  • -100 ≤ num1, num2 ≤ 100

Visualization

Tap to expand
Add Two Integers INPUT num1 12 num2 5 + Input Values: num1 = 12 num2 = 5 Type: Integer (int) ALGORITHM STEPS 1 Receive Inputs Get num1 and num2 values 2 Add Operation Use + operator directly 3 Store Result result = num1 + num2 4 Return Sum Return the result value // Optimal Solution int addTwoIntegers( int num1, int num2) { return num1 + num2; } FINAL RESULT 12 + 5 = Sum 17 OK - Verified! 12 + 5 = 17 Output: 17 Key Insight: This problem demonstrates the simplest arithmetic operation. The optimal solution uses the built-in + operator with O(1) time and O(1) space complexity. No loops or extra data structures needed - direct computation is the most efficient approach. TutorialsPoint - Add Two Integers | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 10
125.0K Views
Very High Frequency
~2 min Avg. Time
4.5K 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