Find the Maximum Difference

See the original problem on HackerRank.

You are given a sequence of integers S, your task is to find a pair i,j of indices with i<j that maximizes the difference S[j]-S[i].


Note that what makes this challenge more interesting is the requirement of order, that i < j (without it you simply need to find the maximum and minimum element of S).


You have to output the value of the maximum difference.

Input Format

An integer N followed by N space separated integers S[i] on a new line.

Constraints

1<N<=10^6

0<=S[i]<=10^4

Output Format

The maximum difference as an integer.

Solutions

This problem has a naive quadratic solution that involves considering every pairs \(i,j\) with \(j>i\) and tracking the maximum. What makes this naive is that it is a bruce force solution that does not consider the input domain or the structure of the problem.

To see the solution, consider what pairs we should consider for the index \(j\). Let’s suppose that \(j\) is the right hand index so we need only look at elements of \(S\) that precede \(j\). The difference \(s[j]-s[i]\) is maximized for the index \(i\) that contains the minimum element of \(s[1],…s[j-1]\).

Therefore, instead of looking over pairs, we need only to keep track of the maximum element preceding any other index. This is linear.

Here is a possible C++ implementation of the described idea:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
int find_max_diff(const vector<int>& in)
{
	auto max = numeric_limits<int>::min();
	auto min = begin(in);
	auto i = next(min);
	while (i != end(in))
	{
		max = std::max(max, *i - *min);
		if (*i < *min) min = i;
		i++;
	}
	return max;
}

int main() 
{
    int N; cin >> N;
    vector<int> v(N);
    copy_n(istream_iterator<int>(cin), N, begin(v));
    cout << find_max_diff(v);
}

The solution above hides two emerging patterns:

  • we calculate a running minimum (min is updated independently at every step)
  • we reduce to the max value on the difference between the current element and the current running minimum

Here is an implementation:

1
2
3
4
5
6
vector<int> mins(v.size());
partial_sum(begin(v), end(v), begin(mins), [](auto l, auto r) { return min(l,r); });
cout << inner_product(begin(v), end(v), begin(mins), 0, 
                      [](auto l, auto r){ return max(l, r); },
                      minus<>{}
                     );

From C++20, ranges’s view::partial_sum can help remove the support array:

1
2
auto mins = view::partial_sum(v, [](auto l, auto r){ return std::min(l, r); });
cout << ranges::max(view::zip_with(std::minus<>{}, v, rg));

It should be clear that mins is a lazy range and not a vector. The entire chain is lazy so the algorithm is one-pass.

Another solution - less efficent and for some people preparatory for the previous solution - consists in using two prefix sums:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int n; cin >> n;
vector<int> v(n);
copy_n(istream_iterator<int>(cin), n, begin(v));
vector<int> s_min(n);
partial_sum(begin(v), end(v), begin(s_min), [](auto... a){return min(a...);});
vector<int> s_max(n);
partial_sum(rbegin(v), rend(v), rbegin(s_max), [](auto... a){return max(a...);});

cout << inner_product(begin(s_max), end(s_max), begin(s_min), 0, 
                     [](auto... a){ return max(a...); },
                     minus<>{});

Just for fun, an alternative solution is based on the Maximum Subarray Sum problem. We first calculate the differences between adjacent elements and then solve the maximum subarray problem on the resulting array.

In C++ we can combine several standard algorithms:

1
2
3
4
5
6
7
8
int n; cin >> n;
vector<int> v(n);
copy_n(istream_iterator<int>(cin), n, begin(v));
adjacent_difference(begin(v), end(v), begin(v));
partial_sum(next(begin(v)), end(v), begin(v), [](auto sum, auto i){
   return std::max(i, sum + i);
});
cout << *max_element(begin(v), prev(end(v)));
We've worked on this challenge in these gyms: modena 
comments powered by Disqus