Given an array of integers, write a method - **maximum gain**. Maximum Gain is defined as the maximum difference between 2 elements in a list such that the larger element appears **after** the smaller element. If no gain is possible, return 0.

`maxGain`

- that returns the
Need a **hand?** Try out these hints, one at a time.

The naive approach would involve using 2 loops that would result in a runtime complexity of O(n^{2}). Let's try and do better than that!

Since the larger element must always appear after the smaller element, this problem can be solved in a single pass. Keep a record of the maximum gain found so far, and the minimum element. When finding the maximum gain, use the difference between the current element and the minimum element found so far.

public static int maxGain(int[] a) { }

**C**

**Java**

**Python**