2's Complement | Practice | GeeksforGeeks

For a given string of binary number bi. Find the two’s complement of it.
Example 1:
Input: bi = 00000101
Output: 11111011
Explaination: 2's complement is reversing all 
the bits of the given number and then adding 
1 to it.
Example 2:
Input: bi = 101
Output: 011
Explaination: Follow the process of figuring 
out 2's complement. This will be the answer.
Your Task:
You do not need to read input or print anything. Your task is to complete the function complement() which takes bi as input parameter and returns the 2's complement of the number.
Expected Time Complexity: O(|bi|)
Expected Auxiliary Space: O(|bi|)
Constraints:
1 ≤ |bi| ≤ 10
0
mayank20211 month ago
C++ : 0.0/1.1
string complement(string bi){              for(int i=0; i<bi.size() ; i++)        {            if(bi[i]=='0') bi[i]='1';            else bi[i]='0';                    }                for(int i=bi.size()-1; i>=0 ; i--)        {            if(bi[i]=='0')             {                bi[i]='1';                break;            }            else bi[i]='0';                    }                        return bi;          }
0
Amar Prakash1 year ago
Amar Prakash
string complement(string bi){        string res = "";        int index = 0;        int n = bi.length() - 1;        for(int i=n; i>=0; i--)        {            if(bi[i] == '1')            {                index = i;                break;            }
        }        for(int i=0; i<index ;="" i++)="" {="" if(bi[i]="=" '1')="" {="" res="" +="0" ;="" }="" else="" if(bi[i]="=" '0')="" {="" res="" +="1" ;="" }="" }="" for(int="" i="index;" i<bi.length();="" i++)="" {="" res="" +="bi[i];" }="" return="" res;="" code="" here="" }="">
0
Saxena Vivek1 year ago
Saxena Vivek
*********python plz check this code below i want help***********class Solution:    def complement(self, bi):        bits="" #empty string        for var in bi:            bits+="1"    #after that deindent for loop        diff=int(bits)-int(bi) # 1s compliment        binary1=("0b"+str(diff))        binary2=("0b"+"001")  # 2s comp = 1s complimenr + binary 1        integer_sum = int(binary1, 2) + int(binary2, 2)        binary_sum = bin(integer_sum)        return (str(binary_sum[2:]))
0
Saxena Vivek1 year ago
Saxena Vivek
*******************python not working **********************        # code here        #x=len(bi)        #print(x)        bits=""        for var in bi:            bits+="1"        #print(type(bits))            diff=int(bits)-int(bi)        #print(diff) # 1s compliment        binary1=("0b"+str(diff))        binary2=("0b"+"001")  # 2s compliment = 1s complimenr + binary 1        integer_sum = int(binary1, 2) + int(binary2, 2)        binary_sum = bin(integer_sum)        return (str(binary_sum[2:]))
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.