Whats wrong with this code? (BINADD)

Problem:

#include<bits/stdc++.h>
using namespace std;
int check(string &a, string &b){
while(a.size()<b.size()) a += ‘0’;
while(a.size()>b.size()) b += ‘0’;
a += ‘0’, b += ‘0’;
int carry = 0, k = 0, ans = 0;
bool flag = false;
for(int i=a.size()-1; i>=0; i–){
if(a[i] == ‘1’ && b[i] == ‘1’){
// Carry genration
if(flag)
k = 0;
flag = true;
}
else if((a[i]== ‘0’ && b[i] == ‘1’) || (a[i] == ‘1’ && b[i] == ‘0’)){
// Carry propogation
if(flag)
k++;
}
else{
flag = false;
k = 0;
}
ans = max(ans, k);
}
return ans + 1;
}
int main(){
int t;
cin >> t;
while(t–){
string a, b;
cin >> a >> b;
if(count(b.begin(), b.end(), ‘0’) == b.length()){
cout << 0 << endl;
continue;
}
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
cout << check(a, b) << ‘\n’;
}
}

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

https://www.codechef.com/viewsolution/28385052

public static void main (String[] args) throws java.lang.Exception
{
	// your code goes here
	Scanner in=new Scanner(System.in);
	int t = in.nextInt();
	in.nextLine();
	for(int i=0;i<t;i++) {
	    String s1=in.nextLine();
	    String s2=in.nextLine();
	    int a=Integer.parseInt(s1,2);
	    int b=Integer.parseInt(s2,2);
	    
	    int count=0;
	    while (b>0) {
	        int u=a;
	        a=a^b;
	        b=2*(u&b);
	        count++;
	    }
	    System.out.println(count);
	}
}

This is how I did it. Although I only got 20 points.

That’s just plain brute Force lol

when you add leading zeros to one of the strings to make them equal size you add them in the end of the string insted of the beginning

Thanks mate! Didn’t notice that

Also, please refrain from using a = ‘0’+a multiple times, as the + operator takes linear time, you would end up getting a ‘TLE’. I would recommend using resize or something similar.