Skip to content

Commit 154068e

Browse files
Sum of two integer (codedecks-in#169)
* Create Sum of Two Integers * Update Readme * Add time complexity and space complexity
1 parent d4a56b2 commit 154068e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Java/Sum_of_two_integers.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time-Complexity:- -> O(1) constant as 32 is the number of bits at most we will have to bit shift until carry is zero.
2+
// Space-Complexity:- O(1)
3+
4+
5+
class Solution {
6+
public int getSum(int a, int b) {
7+
8+
int xor=a ^ b;
9+
int carry= a & b;
10+
if(carry == 0)
11+
return xor;
12+
else
13+
return getSum(xor,carry<<1);
14+
}
15+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8484
| ---- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- |
8585
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
8686
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](./Python/single-number-ii.py)<br/> [C++](./C++/Single-Number-II.cpp) | _O(n)_ | _O(1)_ | Medium | | |
87-
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) <br/> [C++](./C++/Single-Number-III.cpp) | _O(n)_ | _O(1)_ | Medium | | |
87+
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) <br/> [C++](./C++/Single-Number-III.cpp) | _O(n)_ | _O(1)_ | Medium | | |
88+
| 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Java](./Java/Sum_of_two_integers.java) | _O(1)_ | _O(1)_ | Medium |
8889
| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java) <br/> [C++](./C++/Number-Complement.cpp) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) |
8990
| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) <br/> [C++](./C++/Detect-Capital.cpp) | _O(n)_ | _O(1)_ | Easy | | |
9091
| 1486 | [XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/) | [Java](./Java/xor-op-in-array.java) | _O(n)_ | _O(1)_ | Easy | | Using XOR |

0 commit comments

Comments
 (0)