Given an array containing only zeros and ones, find the index of the zero that, if converted to one, will make the longest sequence of ones.

For instance, given the array:

```
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1]
```

replacing the zero at index 10 (counting from 0) forms a sequence of 9 ones:

```
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1]
                  '------------^------------'
```


Your task is to complete the function that determines where to replace a zero with a one to make the maximum length subsequence.


**Notes:**
- If there are multiple results, return the last one:

 `[1, 1, 0, 1, 1, 0, 1, 1] ==> 5`


- The array will always contain only zeros and ones.


Can you do this in one pass?

Difficulty: introductory

Starter code:

def replace_zero(arr):
	

Solutions:
def replace_zero(arr):
    m, im, i, lst = 0, -1, -1, ''.join(map(str,arr)).split('0')
    for a,b in zip(lst,lst[1:]):
        i += len(a) + 1 
        candidate = len(a)+len(b)+1
        if m <= candidate:
            im, m = i, candidate
    return im

def replace_zero(arr):
    ret = [0,0]
    arr = [None] + arr + [None]
    for i,e in enumerate(arr):
        if type(arr[i])==int and not e and any((arr[i+1],arr[i-1])):
            mx = (sm(arr[:i]) + sm(arr[i+1:][::-1]))
            ret = [ret,[mx, i-1]][mx>=ret[0]]
    return ret[-1]
    
def sm(arr, c=0):
    i = len(arr)-1
    while arr[i]:
        i -=1
        c +=1
    return c

def replace_zero(lst):
    z = [-1] + [i for i, n in enumerate(lst) if not n] + [len(lst)]
    return max((z[i+1]-z[i-1], z[i]) for i in range(1, len(z)-1))[1]


import itertools

def replace_zero(arr):
    xs = list(itertools.chain.from_iterable([length if key else 0] * length
        for key, grp in itertools.groupby(arr)
        for _, length in [(key, sum(1 for _ in grp))]
    ))
    return max(
        (0, i) if x else (1 + (xs[i-1] if i > 0 else 0) + (xs[i+1] if i < len(xs)-1 else 0), i)
        for i, x in enumerate(xs)
    )[1]

def replace_zero(arr):
    # (len, index)
    best_len, best_idx = 0, 0
    curr_len, curr_idx = 0, None

    for i, e in enumerate(arr):
        if e == 1:
            curr_len += 1
        else:
            if curr_len >= best_len:
                best_len, best_idx = curr_len, curr_idx

            if curr_idx is None:
                curr_len += 1
            elif i - curr_idx < 2:
                curr_len = 1
            else:
                curr_len = i - curr_idx
            curr_idx = i

    if curr_len >= best_len:
        best_len, best_idx = curr_len, curr_idx

    return best_idx

def replace_zero(arr):
    labels=[]
    max=1
    ans=-1
    # find all the indices that correspond to zero values
    for i in range(len(arr)):
        if arr[i]==0:
            labels.append(i)
    # try each index and compute the associated length
    for j in range(len(labels)):
        arr[labels[j]]=1
        count=1
        end=0
        # check how long the sequence in on the left side.
        k=labels[j]
        while end==0 and k!=0:
            if arr[k-1]:
                count=count+1
                k=k-1
            else:
                end=1
        end=0
        k=labels[j]
        # check how long the sequence in on the right side.
        while end==0 and k!=len(arr)-1:
            if arr[k+1]:
              count=count+1
              k=k+1
            else:
                end=1
        # restore the selected element to zero and check if the new value is a max.
        arr[labels[j]]=0
        if(count>=max):
            ans=labels[j]
            max=count
    return(ans)



def replace_zero(arr):
    max_idx, count = None, 0
    left = idx = right = 0
    for i, n in enumerate(arr + [0]):
        if n:
            right += 1
        else:
            if left + right >= count:
                max_idx, count = idx, left + right
            left, idx, right = right, i, 0
    return max_idx

def replace_zero(a):
    li1 = []
    for i, j in enumerate(a):
        if j == 0:
            temp = a[:]
            t,t1,li,temp[i] = i,i+1,[],1
            while t >= 0 and temp[t] == 1 : li.append(temp[t]) ; t -= 1
            while t1 <= len(a) - 1 and temp[t1] == 1 : li.append(temp[t1]) ; t1 += 1
            li1.append([len(li), i])
    return sorted( li1, key=lambda x: (-x[0], -x[1]) )[0][1]

import itertools as it

def iter_len(iterator):
    return len(list(iterator))

def window (iterable, length):
    iterators = [it.islice(iterator, idx, None) for idx,iterator in enumerate(it.tee(iterable, length))]
    return zip(*iterators)
    
def replace_zero(arr):

    def accumulate_group(total, group):
        _, prev_length, prev_begin = total 
        key, length, _ = group
        return key, length, prev_begin+prev_length
        
        
    groups = ((key, iter_len(group), 0) for key, group in it.groupby(arr))
    empty = [(None, 0, 0)]
    indexed_groups = it.accumulate(it.chain(empty,groups,empty), accumulate_group)
    
    is_zero = lambda group: group[0] == 0
    is_one = lambda group: group[0] == 1
    group_len = lambda group: group[1]
    first_pos = lambda group: group[2]
    last_pos = lambda group: group[2] + group[1] - 1
    revelant = lambda triple: is_zero(triple[1])
        
    triples = filter(revelant, window(indexed_groups,3))
    
    def solutions (triples): 
        for left,this,right in triples:
            if group_len(this) == 1:
                yield group_len(left)+1+group_len(right), first_pos(this)
            else:
                yield group_len(left)+1, first_pos(this)
                yield group_len(right)+1, last_pos(this)
    
    length, positions = max(solutions(triples))
    return positions

def replace_zero(arr):
    z = [0]+[i for i in range(len(arr)) if not arr[i]] + [len(arr)]
    sums = sorted([[sum(arr[z[i]:z[i+2]]),z[i+1]] for i in range(len(z)-2)])
    return sums[-1][1]