In the following input::
[3,6,9,1]
My Output:
6
Expected:
3

I did not understand why the expected is 3. Because, the maximum gap in sorted form is 9 -3 = 6. May be my understanding is wrong. Can anybody please explain the problem with example?

Sorted, the array is [1,3,6,9]. There's no gap from 3 to 9 because there's the 6 between them. And if you ignore that, why don't you also ignore the 3 and think the biggest gap is 9-1=8?

Thanks StefanPochmann. Initially, i thought that the problem is to find the maximum gap in the unsorted array. Since, 1 comes after 9, so, it should not be counted since it is not in the sorted form inside the unsroted array. Anyway, now I understand the problem.