Find maximum difference between two elements in the array

Given an array of integers, find the maximum difference between two elements in the array such that the index of smaller element is also smaller than the index of the larger element. Return the difference.

Example Solution

Let array arr equal to [ 2, 7, 9, 5, 1, 3, 5 ]. The maximum difference is between the pair 1 and 9. However the index of 1 is bigger than the index of 9, thus the biggest difference is between 2 and 9. The solution should return 7.

Contributed by Murat Sütunç