192k views
2 votes
Compute the distance between the positions of the first and last zero element in the given array. For example, if the array is

3 0 1 0 4 9 5 0 6
your algorithm should yield 7 - 1 = 6.

User Marse
by
8.3k points

1 Answer

3 votes

Final answer:

To compute the distance between the first and last zero elements in the array, iterate through the array and find their indices. Subtract the indices to find the distance.

Step-by-step explanation:

To compute the distance between the positions of the first and last zero elements in the given array, you can iterate through the array and keep track of the indices where zeros are found. Once you have the indices of the first and last zero elements, you can subtract them to find the distance.

For example, in the array 3 0 1 0 4 9 5 0 6, the first zero is at index 1 and the last zero is at index 7. The distance between them is 7 - 1 = 6.

User Doptrois
by
8.4k points