Add binary
Problem to add the binary values.
Example-1:
Input : a = "100" b = "11" Output : "111" Explain : "100" decimal value is 4."11" decimal value is 3.So 4+3=7. The binary value of 7 is "111".
Example-2:
Input : a = "101" b = "101" Output : "1010" Explain : "101" decimal value is 5."101" decimal value is 5.So 5+5=10. The binary value of 10 is "1010".
Solution
a = "100" b = "11" a = a[::-1] b = b[::-1] sum = 0 k = 0 for i in range(len(a)): if(a[i] == "1"): sum += 2**i for i in range(len(b)): if(b[i] == "1"): k += 2**i a1 = sum+k b1 = bin(a1) c = b1[2:] print(c)
Output
111