1's Complement | Practice | GeeksforGeeks

Given an N bit binary number, find the 1's complement of the number. The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa).
 
Example 1:
Input:
N = 3
S = 101
Output:
010
Explanation:
We get the output by converting 1's in S
to 0 and 0s to 1

Example 2:
Input:
N = 2
S = 10
Output:
01
Explanation:
We get the output by converting 1's in S
to 0 and 0s to 1


Your Task:  
You don't need to read input or print anything. Your task is to complete the function onesComplement() which takes the binary string S, its size N as input parameters and returns 1's complement of S of size N.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
 
Constraints:
1<=N<=100
0
rajakumar767851 week ago
string onesComplement(string S,int N){       //code here       string ans="";       for(int i=0;i<S.length();i++){           if(S[i]=='0'){               ans=ans+'1';           }           else{               ans=ans+'0';           }       }       return ans;   }
0
atulharsh274Premium1 week ago
int i=0;       while(i<S.length()){           if(S.at(i)=='0'){           S.at(i) = '1';           }           else S.at(i) = '0';           i++;       }       return S;
 
0
cshubham4391 month ago
String s = "";
        for(int i = 0; i<N; i++){
            if(S.charAt(i)=='0'){
                s+='1'+"";
            }
            else{
                s+='0'+"";
            }
        }
        return s;
0
imranmbhd24122 months ago
 
string onesComplement(string S,int N) {       string x;       for(int i = 0; i < N;i++) {           if(S[i] == '0') x+='1';           if(S[i] == '1') x+='0';       }       return x;
}
 
0
md78692nawab3 months ago
class Solution{   static String onesComplement(String S,int N){       String s="";  for(int i=S.length()-1;i>=0;i--)  {      char ch=S.charAt(i);      if(ch=='1')      {          s='0'+s;      }else      {          s='1'+s;      }  }       return s;   }}
0
abythankachan3143 months ago
PYTHON CODE:
time taken : 0.0/1.0
def ones Complement(self,S,N):
list1=list (S)
list2=[ ]
ans=' '
for t in list1 : 
     if t=='0':
          list2.append('1')
     elif t=='1':
          list2.append('0')
for j in list2:
       ans+=j
return ans
 
0
raisanjeeb423 months ago
Traverse through a loop from 0 to N-1
If s[i] is 0 then make it 1
and if s[i]=1 then make it 0 
 
Here is the code but do try before looking at the solution
 
 string onesComplement(string S,int N){       //code here                           for(int i=0;i<N;i++){           if(S[i]=='0'){               S[i]='1';           }           else S[i]='0';       }       return S;          }
+2
surajpatil39413 months ago
  // traverse the string upto n      
for(int i =0; i<N; i++)       {     // taking string array      
if(S[i] == '1')           {               cout<<"0";           }           else           {               cout<<"1";           }                  }
0
chaitanya19bcn70834 months ago
String result ="";       char[] ch = new char[S.length()];              for (int i = 0; i < S.length(); i++) {           ch[i] = S.charAt(i);       }             for(int i =0;i<N;i++)      {          if (ch[i]=='0'){          result += '1';          }                else{          result += '0';          }            }      return result;
0
gkumars29564 months ago
class Solution{ public:   string onesComplement(string S,int N){          string ans ="";       for(int i =0;i<N;i++)       {           if (S[i]=='0')           ans+='1';                         else           ans+='0';              }       return ans;   }};//c++ approch
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.