need help to solve stock market puzzle in java for max profit : need max profit. 

what i can modify to get the max profit if i can only buy once and sell once. 
means if i buy at 5 and sell at 150 then its max profit. 

Currently what is have done is buy when price is less than next day ,and sell if price is more than next day. 

as obvious We have to keep in mind we can sell only after we buy, means sell index can not be before buy index.
 
what i have done so far is : 

    package com;
    
    public class Stock {
    
    	public static void main(String[] args) {
    		
    		int[] prices = {20,10,70,80,5,150,67};
    		int length = prices.length-2;
    		int buy=0;
    		int sell=0;
    		int buyIndex=-1;
    		int sellIndex=-1;
    		
    		int i=0;
    		for (i =0 ; i<=length ;i++ ){
    			
    			// buy logic start
                if(prices[i]<prices[i+1]){
                	if(i>buyIndex){
                		buy= prices[i];
                    	buyIndex=i;	
                    	System.out.println("buy"+buy);
                    	System.out.println("buyIndex"+buyIndex);
                	}            	
                } // buy logic finish
                
                
             // sell logic start
                if(buy!=0 && i>buyIndex ){
                	System.out.println("inside sell logic");
                	if(prices[i]>prices[i+1]){
                		sell = prices[i];
                		sellIndex = i;
                		System.out.println("sell"+sell);
                		System.out.println("sellIndex"+sellIndex);
                		
                	}
                } // sell logic end
    		} // for loop end
    
    	} // main end
    
    }

out put is 

    buy10
    buyIndex1
    buy70
    buyIndex2
    inside sell logic
    sell80
    sellIndex3
    buy5
    buyIndex4
    inside sell logic
    sell150
    sellIndex5

Please help. 
