Author: Kamil Debowski Difficulty:Cakewalk PreRequisites:None Problem StatementGiven a string $S$. Each character of $S$ is a digit '0' or '1'. You need to check if all the '1' digits form a single nonempty segment(consecutive subsequence) in the string. Subtask 1Length of $S$ in this subtask will be not more than 50. If segment of '1's exists, it should be consecutive. Let's iterate over left bound of segment and right bound of segment, and check if only '1's lies inside this segment, and only '0's are out of the segment. Complexity of such algorithm is: $O(S^3)$ Subtask 2Define $L$  leftmost position of digit '1' in $S$, $R$  rightmost position of digit '1' in $S$. Corner case is: when all characters in $S$ are '0's, then answer is "NO" as the segment from '1's is empty. If exists at least one digit '1' in $S$ then all characters between $L$ and $R$ must be equal '1', otherwise subsegment from '1's is not consecutive. How to check it? Using one simple "if", $if(RL+1 = C)$, where $C$ is frequency of '1's in $S$. Total complexity of this algorithm is $O(S)$. Solution:Setter's solution can be found here Please feel free to post comments if anything is not clear to you.
This question is marked "community wiki".
asked 29 Apr '17, 18:11

I cant find error in my code.....it gives correct answer for half of the test cases ..... https://www.codechef.com/viewsolution/14151559 Here is my solution.... include<bits stdc++.h="">using namespace std; int main() { int n;scanf("%d",&n); while(n) { char s[1000005]; scanf("%s",s); bool trig=false,done=false; int i; for(i=0;i<strlen(s);i++) { if(trig){if(s[i]=='0'){trig=false;}} else{if(s[i]=='1'&&done){puts("NO");break;}else if(s[i]=='1'){trig=true;done=true;}} } if(i==strlen(s)&&done){puts("YES");} } return 0; } answered 08 Jun '17, 16:41

your code is not printing anything if string doesn't contain any 1's. try this input 1 00000 answered 08 Jun '17, 16:59

Thanx for the bug...got it corrected. answered 08 Jun '17, 17:03

include<stdio.h>int main(){ int t; long int s,q,r; scanf("%d", &t); while(t){ scanf("%ld",&s); if(s==0) printf("NO \n"); else{ q=s; while(q%10==0) q/=10; r=1; while(r==1){ r=q%10; q=q/10; } if(q!=0) printf("NO \n"); else printf("YES \n"); } } return 0; } what is wrong with this solution? Even codechef compiler is giving the expected output (as of the test cases mentioned) but on submitting, I receive WA error. answered 06 Jun '18, 15:43
