Can anyone suggest what is the error in this code.

#include

using namespace std;

int main()
{
int t;
cin>>t;
while(t–){
int n,x,y,i,o=0,p=0;
cin>>n>>x>>y;
string s;
char c;
cin>>s;
//p = n;

    if(x>y){
        for(i=0; i < n; i++){
            if(s[i] == '1'){
                p++;
            }
        }
        for(i = 0; i < n; i++){
            if(i < p){
                s[i] = '1';
            }
            if(i >= p){
                s[i] = '0';
            }
        }
    }
    if(y>x){
        for(i=0; i < n; i++){
            if(s[i] == '0'){
                p++;
            }
        }
        for(i = 0; i < n; i++){
            if(i < p){
                s[i] = '0';
            }
            if(i >= p){
                s[i] = '1';
            }
        }
    }
    for(i = 0; i < n; i++){
        if(s[i] == '0' && s[i+1] == '1'){
            o = o + x;
        }
        if(s[i] == '1' && s[i+1] == '0'){
            o = o + y;
        }
    }
    cout<<o<<endl;
}

return 0;

}

Check the following code for hint about the error in your code.

Accepted

1 Like