Array - Range Question


I’m relatively new when it comes to working with arrays. For my current project, I’m having trouble finding a solution to the problem below.

Take an array [1, 1, 1, 0, 0 ,0, 0, 0, 1, 1] - Call this ARRAY
I want to find how the large the gap of 0’s are within this example array or put another way, how many positions is the last occurring 1 to the next 1 that occurs after a 0.
My initial thoughts were
for (i = 0; i < 10; i++)
_ if (ARRAY == 1) //The idea to look at 1’s and position the last 1 to occur as iMIN_
* { //Since the continue function wont work unless another 1 occurs*
* continue; //When it continues after a gap of 0’s in theory (I think)*
* } // the next i (position value), will be a higher number than iMIN*
* iMIN = i; // Simply take the difference and that is the range*
//Some more code to assign the iMAX
The trouble I’m having currently is assigning an iMAX without reassigning the iMIN.
Anything helpful advice would be appreciated.

Please use code tags.

Are you looking for the length of the first run of zeroes or the longest run of zeroes?

Or the distance from the first zero to the last zero?

It should not be necessary to use the continue statement. Just be careful with your if...else logic.

While i is looking at a zero, advance max to i. If you see a 1, record that length and move both min and max up to i.

You need to define the problem better:

  • Will there always be a “gap” of consecutive ‘zeros’?

  • Will there only be one “gap” of consecutive ‘zeros’?

  • What do you want to do if the “gap” starts with the first element of the array (i.e. there’s no leading ‘one’)?

  • What do you want to do if the “gap” extends through the last element of the array (i.e. there’s no trailing ‘one’)?

  • What do you want to do if BOTH of the previous conditions ocurr (i.e. the array is all ‘zeros’)?

This isn't completely thought out but:

initialize maxGap to zero
initialize gapCounter to zero
start searching
whenever you find a '1', reset gapCounter
if a '0' is found, increment gapCounter
when a '1' is found after a '0', compare gapCounter to maxGap and store gapCounter in maxGap if it is - reset gapCounter

If you need to know where the gap lies, record the loop counter value when an initial '0' is found.

Who knows? It might work!

Consider this pseudo code

counter = 0

for each element in the array:
   if the value == 0,
      if the position == 0,
         break;   //found 0 at array[0] -> we are done
      if (counter)

print counter //counter should have the gap of 0's

How would do do it if you didn't have a computer, and someone was reading out the numbers to you one at a time?