## Zeros And Ones

### October 17, 2017

Our windowing algorithm scans the array from left to right. Indices *lo* and *hi* point to the beginning and end of a subsequence of an array that contains a single zero. As we scan, if the new array element causes us to have two zeros in the subsequence (*count* = 2) we reset the window. *prev2* and *prev1* are the indices of the two most recent zeros. The maximum is reset whenever the window becomes larger than the current maximum. Here’s the code:

(define (zero xs) (let ((max-count 0) (max-index -1) (prev2 -1) (prev1 -1) (count 0) (lo 0)) (do ((hi 0 (+ hi 1)) (xs xs (cdr xs))) ((null? xs) max-index) (when (zero? (car xs)) (set! prev2 prev1) (set! prev1 hi) (set! count (+ count 1))) (when (= count 2) (set! lo (+ prev2 1)) (set! count 1)) (when (< max-count (- hi lo -1)) (set! max-count (- hi lo -1)) (set! max-index prev1)))))

And here are two examples:

> (zero '(1 0 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 1 1)) 10 > (zero '(0 0 1 0 1 1 1 0 1 1)) 7

Our algorithm takes O(*n*) time and O(1) space. You can run the program at https://ideone.com/PaCY31.

Advertisements

Here’s a solution in C99. The algorithm is O(n) time, O(1) space. It considers sub arrays have three zeros, one on each end. It returns the second zero of the longest such sub array. For the algorithm to work properly, indices -1 and n are treated as if there were zeros there.

Output:

Clarification: It considers sub arrays that have three zeros, with one of the zeros at the beginning and one of the zeros at the end.

Clarification: It returns the index of the second zero of the longest such sub array (index with respect to the entire input array).

How do we know the number of total characters that is 1 and 0 the array contains?

here is another solution in c

having had a look at Daniel’s code, realized they are quite the same.

sorry for posting repetitive solution.

Here’s a Haskell version.

@Himanshu I don’t think we should need to know any of those things. The array could be empty (i.e. have 0 length), it could be all 0s, all 1s, or any combination of 0s and 1s.

Here’s a python solution:

Zeros and Ones

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.

Array1 = [1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1]

print(“Initial array:”, Array1)

Step 1: Find indicies of all Zeros in Array1

L = len(Array1)

zeros = []

for i in range(L):

if Array1[i] == 0:

zeros.append(i)

print(“Indicies of all zeros:”, zeros)

Step 2: Number of Ones before and after each zero is equal to differences in index values to the next and previous zero minus one.

L_z = len(zeros)

before = []

after = []

total = []

for i in range(L_z):

if i == 0: # The number of ones before the first zero is equal to its index

valb = zeros[i]

vala = zeros[i+1]-zeros[i]-1

elif i == (L_z-1): # The number of ones after the last zero is equal to its index minus array length minus one.

vala = L-zeros[i]-1

valb = zeros[i]-zeros[i-1]-1

else:

valb = zeros[i]-zeros[i-1]-1

vala = zeros[i+1]-zeros[i]-1

print(“Number of Ones before each Zero:”, before)

print(“Number of Ones after each Zero:”, after)

print(“Number of Ones before AND after each Zero:”, total)

Step 3: Find index of maximum value

index_max = total.index(max(total))

print(“\nThe zero to replace is in index number”,zeros[index_max])

Trying to figure out the proper way to format this…