https://www.codechef.com/problems/CSUB

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
string str;
cin>>str;

char char_array[n+1];
strcpy(char_array,str.c_str());

int m=0;
for(int j=0;char_array[j]!='\0';j++)
{
    if(char_array[j]=='1')
    {
        m++;
    }
}
cout<<m*(m+1)/2<<"\n";
}

}

what is the error in the code giving wrong answer’s for the test cases, base test case passed

Make m a long long int. M*m will become 10^10 and overflow.
Also
Post link in question not title, and use three backticks
` ``to format your code.

1 Like