Resultant String
Time Limit: 2 sec
Memory Limit: 128000 kB
Memory Limit: 128000 kB
Problem Statement
You are given two binary strings of length N. You have to output a resultant string. The resultant string will be calculated in the following way:
- > For every position i, if the ith character of the first string is '1' and the second string is '0', the resultant string will have '1'. Also, for every position i, if the ith character of the first string is '0' and the second string is '1', the resultant string will have '1'. Rest, all the characters of the resultant string will be '0'. Find the resultant string;
- > For every position i, if the ith character of the first string is '1' and the second string is '0', the resultant string will have '1'. Also, for every position i, if the ith character of the first string is '0' and the second string is '1', the resultant string will have '1'. Rest, all the characters of the resultant string will be '0'. Find the resultant string;
Input
The first line of the input contains a single integer N.
The next two lines of the input will contain a string of size N each.
The next two lines of the input will contain a string of size N each.
Constraints:
1 <= N <= 105
All characters of the string will be either '1' or '0'
1 <= N <= 105
All characters of the string will be either '1' or '0'
Output
Print the resultant string.
Example
Sample Input:
5
10110
01101
Sample Output:
11011
Guys if the test case is not passing take a higher Datatype, double or long in place of integer.
Use Try and catch Block to remove nzec error , but its not working.
Solutions
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int digit = sc.nextInt();
int b1=sc.nextInt();
int b2=sc.nextInt();
int result =0;
while(digit>0){
int c = b1%10;
b1 = b1/10;
int c1 = b2%10;
b2 = b2 / 10;
if (c==1 && c1==1) {
result = 0;
}
else{
result = 1;
}
System.out.print(result);
digit--;
}
}
}
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main(String[] args) throws IOException {
try {
Scanner sc = new Scanner(System.in);
int digit = sc.nextInt();
int b1 = sc.nextInt();
int b2 = sc.nextInt();
int result = 0;
while (digit > 0) {
int c = b1 % 10;
b1 = b1 / 10;
int c1 = b2 % 10;
b2 = b2 / 10;
if (c == 1 && c1 == 1) {
result = 0;
} else {
result = 1;
}
System.out.print(result);
digit--;
}
}
catch (Exception e){
return;
}
}
}