just giving example so that there will may not be any need to refer gfg link

A = 1 **0 1 0 1 1** 0 **1 0 1** 0

B = 0 **0 1 0 1 1** 1 **1 0 1** 1

A-B = 1 **0 0 0 0 0** -1 **0 0 0** -1

position 1 2 3 4 5 6 7 8 9 10 11

Now there may be more that 1 continuous chain of 0 , how to find max of all of them?

take two pointer i and j . initialize i to 1

check if A-B[i] == 0 {means is this start of chain of zero} ,

if yes then do j=i & increment j till A-B[j] is 0 or j hits end of array

if A-B[j] becomes != 0 then just note max length of chain of 0 found till now i.e. j- i

also make i = j+1 so that we can find another chain of 0

if A-B[i] != 0 then increment i

If you observe carefully for every element in A we are doing comparison with that element only one time.

Hence time complexity will be linear

In example that I have given above

1. first i will be incremented to 2

2. we found A-B[2] == 0 hence initialize j = i =2

3. increment j till A-B[j] == 0 , j will be incremented till j=7 where A-B[7] != 0

4. Find longest sequence of 0 found till now j - i -1 = 7-2 = 5

assign this to some variables , also assign i , j-1 to some variables

5. make i = j+1 = 7 +1 =8

6. check A-B[i] == 0 , yes , hence make j=i

7. increment j till A-B[j] is zero , j will be incremented till j=11 , where A-B[j] != 0

8. Hence find sequence of 0 encountered till now i.e. j - i = 11-8 = 3

9.Check if previously found sequence length is smaller that this , -> No

10.Now we also reached till end , hence max length will be 5 and span will be 2 to 6