Just another Divide-And-Conquer question - but somehow different
Your solution doesn't work since it could output $i_2,j_1$ which isn't legal since $i_2 &gt; j_1$. Your code also has a few bugs &mdash; are you returning indices or stock prices?

The idea behind the divide and conquer solution is that the optimal solution is of one of three forms:


Buy and sell within the first half.
Buy and sell within the second half.
Buy within the first half and sell within the second half.


The first two are taken care of by the recursion, and for the third you just need to compute the minimum of the first half and the maximum of the second half.

There is also a simple (and faster) solution not involving any kind of recursion or sorting. The idea is to scan the array from left to right, keeping at any given point the smallest value seen so far. At any given point in time, you can consider the gain from buying at the smallest price seen so far and selling at the current price. The maximum over all these possibilities is the optimal strategy. This algorithm takes linear time, and can even be implemented online.