I am solving a coding challenge and my code failed in test cases with large number of inputs due to timeout.
I am using a nested for loop to find the minimum difference in an array of integers with respect to their order (sorting isn't an option). For example: minimum difference in this array: {20, 7, 8, 2, 5} is (7 - 5 = 2) not (8 - 7 = 1).
I know that using nested for loops are bad when it comes to execution time. I've searched a lot for an alternative to using nested for loops in this case and failed to find any.
Is there a way to implement this algorithm without using nested for loop?