I try to output the length of the longest possible sequence of 1s after flipping the second "0" of a given binary sequence. I think I've found the right algortithm for that but I keep struggling with an IndexOutOfRangeException
Here's my code
string b = Console.ReadLine();
//char temp;
int c = 0;
int i = 0, l = 0, j = 0;
l = b.Length - 1;
char[] charArr = b.ToCharArray();
while (l > 0)
{
if (charArr[i] == '0')
{
for (j = i; j <= l; j++)
{
c++;
if (charArr[j] == '0')
{
//temp = '1';
//char temp1 = temp;
charArr[j] = '1';
}
}
}i++;
}
b = string.Join("", charArr);
Console.WriteLine(c);