Solution For Add Two Integers
Problem Statement:
Given two integers a and b, return the sum of them.
Constraints:
-1000 <= a, b <= 1000
Solution:
The problem is quite straightforward, we just need to add two integers. We can use the ‘+’ operator for this.
We can simply write a function that takes two integer parameters and returns their sum. The solution in Python is given below:
def add_two_integers(a: int, b: int) -> int:
return a + b
We can also solve this problem using the bit-wise operator.
def add_two_integers(a: int, b: int) -> int:
while b != 0:
carry = a & b
a = a ^ b
b = carry << 1
return a
In the above solution, we are using the bit-wise operators ‘&’, ‘|’ and ‘<<‘. We are using a while loop to perform the addition.
First, we take the bitwise AND of ‘a’ and ‘b’ and assign it to ‘carry’. We then take the bitwise XOR of ‘a’ and ‘b’ and assign it to ‘a’. We shift ‘carry’ one position to the left and assign it to ‘b’.
We then repeat the above steps until ‘b’ becomes 0. Finally, we return the value of ‘a’.
The time complexity of this solution is O(1) and the space complexity is also O(1).
Test Cases:
Let’s test our solution with some test cases:
assert add_two_integers(1, 2) == 3
assert add_two_integers(0, 0) == 0
assert add_two_integers(-1, 1) == 0
Conclusion:
In this problem, we learned how to add two integers using both the ‘+’ operator and the bit-wise operators ‘&’, ‘|’ and ‘<<‘. The problem was quite straightforward and easy to solve.
Step by Step Implementation For Add Two Integers
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { // create a dummy head node to simplify the code ListNode dummyHead = new ListNode(0); // create a pointer to keep track of the current node ListNode curr = dummyHead; // create a carry variable to keep track of the carry int carry = 0; // while both lists are not null while (l1 != null || l2 != null) { // compute the sum of the current digits and the carry int sum = carry; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } // compute the carry carry = sum / 10; // create a new node with the sum % 10 curr.next = new ListNode(sum % 10); // advance the pointer curr = curr.next; } // if there is a carry left over, create a new node with the carry if (carry > 0) { curr.next = new ListNode(carry); } // return the dummy head's next node, which is the head of the sum list return dummyHead.next; } }
def add_two_integers(a, b): """ :type a: int :type b: int :rtype: int """ # Your code here return a + b
var addTwoIntegers = function(a, b) { // Check if both inputs are valid integers if (!Number.isInteger(a) || !Number.isInteger(b)) { return null; } // Initialize result let result = 0; // Add each digit of the two integers starting from the ones digit for (let i = 0; i < Math.max(a.toString().length, b.toString().length); i++) { // Get the digit in the ones place for each integer const aDigit = a.toString()[a.toString().length - 1 - i] || 0; const bDigit = b.toString()[b.toString().length - 1 - i] || 0; // Add the digits and carry over any remainders result += (parseInt(aDigit) + parseInt(bDigit)) % 10; // If there is a remainder, carry it over to the next digit if (parseInt(aDigit) + parseInt(bDigit) >= 10) { result += 1; } // Move to the next digit place result *= 10; } // Return the result divided by 10 to remove the extra digit place return result / 10; };
class Solution { public: int addTwoIntegers(int a, int b) { // Initialize result int result = 0; // Determine how to iterate over bits int i = 0; for (int i = 0; i < 32; i++) { // Get bit values int bit_a = (a >> i) & 1; int bit_b = (b >> i) & 1; // Toggle the bit in result if both input bits are 1 if (bit_a == 1 && bit_b == 1) { result ^= (1 << i); } // Else, add the bit from a or b to result (bitwise OR) else { result |= (bit_a | bit_b) << i; } } return result; } };
public class Solution { public int AddTwoIntegers(int a, int b) { // return sum of a and b return a + b; } }