To count all 1 's in the string
int cnt_one = 0 ; int max_cnt = 0 , temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { cnt_one ++ ; temp ++ ; } else { max_cnt = max ( temp , max_cnt ) ; temp = 0 ; } } max_cnt = max ( max_cnt , temp ) ;