Traverse the string to find the first and last occurrences of '1'
for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { if ( firstOne == - 1 ) firstOne = i ; lastOne = i ; } } if ( ( firstOne == - 1 ) || ( firstOne == lastOne ) ) { Console . Write ( 0 ) ; return ; }