Simulating going through array from top to bottom in parallel

Hello,
I’ve been thinking about this for a long time, but cannot get any idea how this could work in parallel, in kernel. Lets say I have 2 arrays (table) something like this:

10 0
10 1     // start searching for line with value which is more than 4 larger than 10 (15+)
9  1     // even if here is 1 in second column, ignore, because previous search is not finished
4  1     // even if here is 1 in second column, ignore, because previous search is not finished
7  0     // nothing
15 0     // found 15, which is more than 4 larger than 10, counter++ 
13 1     // start searching for line with value which is more than 4 larger than 13 (18+)
20 0     // found 20, which is more than 4 larger than 13, counter++

what I need to do, is for every line that contains 1 in second column, I need to find closest next line below, with it’s value >4 from original lines value. That I could do something like(lets asume array is 8 long and I have 8 threads):

int counter = 0;
if(array2[threadIdx.x] = 1){
   for(int n = threadIdx.x +1, n < blockDim.x, n++){
       if(array1[n] - array1[threadIdx.x] > 5){
           counter++; end1;
       }
   }
}

I know it’s not very good because of loops, but have no clue how to do it differently.
The main problem I have is, that I don’t want to count(or even try to find next value >5 from orig) those lines with 1, where previous line with 1 hasn’t finished searching for the value >5. That should be the next step, which I have no idea how to do. Originally the counter would be 4, but I want the code to not count from 9 and 4, because if you go from top to bottom, start at 10. If anyone could show me how to make such code with as fast as possible performance, doing this to large arrays. thank you

may be it’s just me, but i don’t even understood what exactly you need. if it’s just question of efficiency, start with naive solution - it may turn out to be good enough. and you will have at least something to share here and can profile it to choose optimization strategy