CSUB - Editorial

What’s wrong with my code?

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

https://www.codechef.com/viewsolution/26129221
can someone pls let me know what mistake i am doing
1.taking input in string
2.declared long long for product
still WA

It has to be nc1 + nc2 where n = no of zeros in string, in more detail, right?

Why this gets a WA even though I took care of overflow?
#include
#include

using namespace std;

int main(){
	int T;
	cin>>T;
	for(int t=1;t<=T;t++){
		int N;
		cin>>N;
		
		char str[1];
		long long int ones=0,substrings=0;
		
		for(int i=0;i<N;i++){
			cin>>str[0];
			if(strcmp(str,"1")==1){
				ones++;
			}
		}
		if(ones==1){
			substrings = 1;
		}
		else{
			substrings = ones*(ones-1);
			substrings = substrings/2;
			substrings = substrings + ones;
		}
		cout<<substrings<<endl;
	}
}

Please provide explanation on how you came up with this formula.

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

    #include <bits/stdc++.h> 

#include
#include
using namespace std;

int main()
{
int t,i,c=0,j=0,n;
string s1,s2;
cin>>t;
while(t–){
cin>>n;
for(i=0;i<n;i++){
cin>>s1[i];
}
for(i=0;i<n;i++){
if(s1[i]==‘1’){
j=i;
while(j<n){
if(s1[j]==‘1’)
{c++; }
j++;
}
}
}
cout<<c<<"\n";
c=0;
}
}

Pls help.Why I am getting runtime ?

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

Edit:

It’s because you’re trying to place chars at the ith index of s1 for i = 0 … n:

for(i=0;i<n;i++){
            cin>>s1[i];
        }

when s1 is empty.

because of this

#include <bits/stdc++.h> 
#include <string>
using namespace std; 
  
int main() 
{ 
    long int t,i,j=0,n;
    long long int c=0;
    string s1;
    cin>>t;
    while(t--){
        cin>>n;
        for(i=0;i<n;i++){
            cin>>s1[i];
        }
        for(i=0;i<n;i++){
            if(s1[i]=='1'){
                j=i;
                while(j<n){
                    if(s1[j]=='1')
                    {
                        c++; 
                    }
                    j++;
                }   
            }
        }
        cout<<c<<"\n";
        c=0;
    }
} 

It is running but on submission it is giving runtime error.

1 Like

Pls explain.I’m not able to understand

Try reading this:

why is my code getting wrong answer CodeChef: Practical coding for everyone

how can “long int count” instead of “int count” make wrong answer right?my submission bcame successful when i used long int count.
https://www.codechef.com/viewsolution/29154571

Where is my code is wrong i donot understand.Please Explain.

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
string str;
cin>>str;
int count=0;
for(int i=0;i<n;i++){
if(str[i]==‘1’){
count++;
}
}
int ans=0;
if(count==1){
ans=count;
}else if(count==2){
ans=3;
}else{
ans=(count+1)*(count);
ans=ans/2;
}
// cout<<count<<endl;
cout<<ans<<endl;
}
return 0;
}

The constraint for n is 1<=n<=10^5 but we had to take long long for storing the count value of 1’s
but count value of 1’s must be less than or equal to 10^5.
Can someone explain me why???

You don’t have to, but it makes it easier to avoid overflow when computing (num1s * (num1s - 1))/2. You could also do e.g.:

#include <iostream>

using namespace std;

int main()
{
    int T;
    cin >> T;

    for (int t = 0; t < T; t++)
    {
        int n;
        cin >> n;

        string binaryString;
        cin >> binaryString;

        int num1s = 0;
        for (const auto bit : binaryString)
        {
            if (bit == '1')
                num1s++;
        }

        cout << (static_cast<long long int>(num1s) * (num1s - 1)) / 2 << endl;
    }
}

Quite often Competitive Programmers will use long long int or an equivalent instead of int for all variables as it can be less error-prone.

2 Likes

Take c as long long int.

Count of 1 can be as high as 10^5 , and when you multiply it n*(n+1), its goes out of range , thus we need to take long long int for n.

Can some check what is the error in my code, i used something like two pointer to count starting 1 and added all the ending 1’s after that pointer
https://www.codechef.com/viewsolution/38096948