Buying and Selling Stocks

 import java.util.*;


class stocks{
    public static void main(String[] args) {

        int prices[]={7,1,5,3,6,4};

        int buy=Integer.MAX_VALUE,sell=Integer.MIN_VALUE;

        int maxprofit=0;

        for(int i=0;i<prices.length-1;i++){

            int j=i+1;

            buy=Math.min(prices[i],buy);

            int profit=prices[j]-buy;

            maxprofit=Math.max(profit,maxprofit);

            j++;


        }

        // int profit=sell-buy;

        System.out.print("maxprofit is "+maxprofit);
       

    }
}

output-

Comments

Popular posts from this blog

Switch case advance usage

Convert first letter to capital letter in java

Inheritance