@@ -0,0 +1,1635 @@
+import unittest%0A%22%22%22%0AGiven an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest contiguous%0Asequence of 1s.%0AInput: 1 1 0 0 1 0 1 1 1 0 1 1 1%0AOutput: index 9%0AInput: 1 1 1 1 0%0AOutput: index 4%0AInput: 01010%0AOutput: index 2%0A%22%22%22%0A%0A%22%22%22%0AApproach:%0A1. The idea is to keep track of 2 indexes - prev_zero and prev_prev_zero.%0A2. If current number is zero, calculate the difference between current index and prev_prev_zero.%0A3. This difference minus 1 is the number of 1s around prev_zero.%0A4. Update max_difference so far by comparing with difference from previous step.%0A5. Return the prev_zero index corresponding to max_difference as the answer.%0A%22%22%22%0A%0A%0Adef index_of_zero_to_get_longest_1s(ones_and_zeros):%0A%0A    prev_zero = -1%0A    prev_prev_zero = -1%0A    end = len(ones_and_zeros)%0A    max_index = -1%0A    max_1s_length = 0%0A%0A    for i in range(end):%0A        if ones_and_zeros%5Bi%5D == 0:%0A            if i-prev_prev_zero %3E max_1s_length:%0A                max_1s_length = i-prev_prev_zero%0A                max_index = prev_zero%0A            prev_prev_zero = prev_zero%0A            prev_zero = i%0A%0A    if end-prev_prev_zero %3E max_1s_length:%0A        max_index = prev_zero%0A%0A    return max_index%0A%0A%0Aclass TestMax1s(unittest.TestCase):%0A%0A    def test_max_1s_length(self):%0A        ones_and_zeros = %5B1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1%5D%0A        self.assertEqual(index_of_zero_to_get_longest_1s(ones_and_zeros), 9)%0A        ones_and_zeros = %5B1, 1, 1, 1, 0%5D%0A        self.assertEqual(index_of_zero_to_get_longest_1s(ones_and_zeros), 4)%0A        ones_and_zeros = %5B1, 1%5D%0A        self.assertEqual(index_of_zero_to_get_longest_1s(ones_and_zeros), -1)%0A

Index of zero to get longest 1s