title: If you know the future prices of a stock, what's the best time to buy and sell?
body: <p>Interview Question by a financial software company for a Programmer position</p>
  
  <blockquote>
    <p>Q1) Say you have an array for which the ith element is the price of a given stock on 
        day i.</p>
    
    <p>If you were only permitted to buy one share of the stock and sell one share 
        of the stock, design an algorithm to find the best times to buy and sell.</p>
  </blockquote>
  
  <p>My Solution :
      My solution was to make an array of the differences in stock prices between day i and day i+1 for arraysize-1 days and then use Kadane Algorithm  to return the sum of the largest continuous sub array.I would then buy at the start of the largest continuous array and sell at the end of the largest
  continous array.</p>
  
  <p>I am wondering if my solution is correct and are there any better solutions out there???</p>
  
  <p>Upon answering i was asked a follow up question, which i answered exactly the same</p>
  
  <blockquote>
    <p>Q2) Given that you know the future closing price of Company x for the next 10 days ,
        Design a algorithm to to determine if you should BUY,SELL or HOLD for every
        single day ( You are allowed to only make 1 decision every day ) with the aim of
        of maximizing profit</p>
    
    <p>Eg: Day 1 closing price :2.24<br>
           Day 2 closing price :2.11<br>
            ...<br>
            Day 10 closing price : 3.00</p>
  </blockquote>
  
  <p>My Solution: Same as above</p>
  
  <p>I would like to know what if theres any better algorithm out there to maximise profit given
  that i can make a decision every single day</p>