Given an array as input A[0,1,2,3…N], in this array two elements A[i] and A[j] are said to be adjacent if there is no element in range (A[i],A[j]) i.e, if A[i]=1 and A[j]=5 then A[i] and A[j] are said to be adjacent if array doesn’t contain element in range (1,5).

Calculate the maximum possible distance j-i where j and i are indices of adjacent element in given array.

Expected time complexity: O(N logN)