Minimize longest continuous streak of $1$s after flipping $m$ $-1$s to $1$s Given an array a of length n of $-1$s and $1$s, and another input $m$, I was supposed to minimize the longest continuous streak of $1$s or $-1$s after flipping $m$ $-1$s to $1$s or vice versa.

I know how to maximize it but can't figure out how to minimize it. I maximize the number of $1's$ by sliding window method. 
Take a window ($wleft$ , $wright$).It covers given array from index $wleft$ to $wright$.
I represent $-1$ with $0$ for ease.Suppose number of zeros in window in $wzeros$.

Algorithm :

while wright &lt; length of array {

     wzeros &lt;= m: increment wright

     wzeros&gt;m: increment wleft 

  // Update the max window size during this whole process. 

        int temp = wright-wleft;
        if(temp &gt; mx_length){
          mx_length = temp;
          solution_window = wleft;      
        }       
    }


This code maximizes the number of consecutive $1$s whereas I want to minimize the number of consecutive $1$s and $-1($or $0)$s. How do I modify the algorithm/code to achieve so ?
You can use dynamic programming. It is very likely that much faster algorithms exist.

Given a string $w$ of length $n$, for $0 \leq i,j \leq n$ and $0 \leq k \leq m$ you compute the minimal maximum 1-run in $w_1,\ldots,w_i$ given that $k$ many -1s were changed to 1s, and given that $w_1,\ldots,w_i$ ends in a 1-run of length $j$.

This gives an $O(n^2m) = O(n^3)$ algorithm.