CSUB - Editorial

Can someone provide me with a test case for which my code is wrong. It is running perfect still i am getting WA.
https://www.codechef.com/viewsolution/14664852

can someone tell me what’s wrong in this code,thanks in advance.
https://www.codechef.com/viewsolution/15359934

Use char s[n+10]. Always give atleast 1 extra space in case you’re taking input of string as a char array.

AC soln- https://www.codechef.com/viewsolution/15583470

Use long instead of int. Overflow is causing WA

Declare p as long long instead of int. Overflow causing WA

Answered the queries as requested.

I am not sure what is wrong with this code : https://www.codechef.com/viewsolution/16553453

Its executing completely fine with my IDE (DevC++) but CodeChef is showing WA. Can anyone pls help ?

I have solved this problem now ! I used a different approach ! Thanks !

Someone please clarify : We are essentially choosing two 1’s from a string of n 1’s, so basically, we need to do nC2, right? Then by this the solution should be (n*(n-1))/2! Where am I going wrong?

U can also choose the same index twice so nC2+n=(n*(n+1))/2

Hi. I’m not able to understand how and where I went wrong even though it displays the correct answer for the practice cases:

#include
using namespace std;

int main()

{
string s;

unsigned long long int t,n,count,i;
char c;
cin>>t;
for(int o=0;o<t;o++)
{
	cin>>n;
	count=0;
	for(i=0;i<n;i++)
	{
		cin>>c;
		if(c=='1')
			count++;
	}
	if(count%2)
		cout<<((count+1)/2)*count;
	else
		cout<<((count/2)*(count+1));
	
	

}

}

Could someone please explain how we got the formula?

Could someone please explain how we got the formula?

First we have to count the number on 1’s in the string,the to find the possible combinations of no of ones so we use the formula n*(n+1)/2

It could be something like this…

nC1= combinations taken 1 at a time; or individual 1’s are their own start and end point.
Then nC2 = combinations taken 2 at a time.
Adding these two makes up nC1+nC2=n×(n+1)/2

But I think it should be someting like this…
Example: 111
nC1= 1,1,1
nC2=11,11,11 we can’t include last combination in this sequence because only way to move is forward and last sequence groups last index and first index together.

So nC2 is here actually nC2 - 1 combination = 11,11
Now the last one is
nC3 = 111

So total numbers in the list are 111,11,11,1,1,1

So we have 6 numbers which satisfy this condition.
And n×(n+1)/2 is actually 6.

I am not entirely sure on this…
If you guys have any other explanation please share.

Shouldn’t the answer for Case #2 (i.e.10001) be 4. the sub-strings being 11,101,1001,10001,
instead of 3?

@kalpaj12 what you’re saying is called subsets and not substrings. A substring is a contiguous sequence of characters within a string. so, here the valid substrings would be {1}(1st element of array),{1}(last element of array),{1,0,0,0,1}. Hence, Answer will be 3

thanks @shinchan6599

Hi,

I am getting WA. Following is my code. I followed the n(n+1)/2 principle, yet I am not able to clear it.
Kindly point out the mistake.

#include <bits/stdc++.h>

using namespace std;


int main(){
	int t, n, count;
	string tmp;
	cin>>t;
	while(t--){
		cin>>n;
		count = 0;
		cin>>tmp;
		for(string::iterator i = tmp.begin(); i!=tmp.end();++i){
			if(*i=='1'){count++;}
		}
    cout<<(count*(count+1))/2<<endl;
	}
	return 0;
}

@insoumniac hope this helps,

first string: 1111
possible ways of getting string of ‘1’ length = 4(length of string)
possible ways of getting string of ‘2’ length = 4-1 = 3(length of string - 1)
possible ways of getting string of ‘3’ length = 4-2 = 2(length of string - 2)
possible ways of getting string of ‘4’ length = 4-3 = 1(length of string - 3)

We sum the ways: 1+2+3+4 = 10(sum of first 4 numbers)

I guess you saw the pattern and how the formula came