stock prices of a company

This Question Was Asked By Facebook

An array of numbers representing the stock prices of a company in chronological order is given. Write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy it before you can sell it.

Write a function solve that have the following parameter(s):
1.) a = an array of integers

Example
Input:
[9, 11, 8, 5, 7, 10], Output:
5

Explanation
From given stock prices [9, 11, 8, 5, 7, 10] we can see that the stock went to lowest price of $5 before the high price of $10, which would have given us a profit of $5, hence you should return 5, since we can buy the stock at 5 dollars and sell it at 10 dollars.

Constraints
• The length of an array should be greater than 0 and less than 800.
• The elements of an array should be greater than 0 and less than 1000.