For a given string of binary number find the two’s complement of it.
Input :
The first line contains an integer 'T' denoting the total number of test cases.The next T lines contains the string S.
The first line contains an integer 'T' denoting the total number of test cases.The next T lines contains the string S.
Output:
In each separate line the two’s complement string should be output.
In each separate line the two’s complement string should be output.
Constraints:
1<=T<=30
1<=lenght(string)<=10
1<=T<=30
1<=lenght(string)<=10
Example:
Input:
1
00000101
Input:
1
00000101
Output:
11111011
11111011
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
Scanner sc= new Scanner(System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
String s=sc.next();
char [] c=s.toCharArray();
int count=0;
for(int j=0;j<s.length();j++){
if(c[j]=='0'){
count++;
}
}
if(count==s.length()){System.out.println(s);}
else{
for(int i=0;i<s.length();i++){
if(c[i]=='1'){c[i]='0';}
else{c[i]='1';}
}
String s1= new String(c);
int sum=Integer.parseInt(s1,2)+Integer.parseInt("1",2);
System.out.println(Integer.toBinaryString(sum));
}
}
}
}
EXECUTION TIME:0.1s
Comments
Post a Comment