import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {


    public static int maxProfit(int [] A){
        int n = A.length;
        int max = A[n-1];
        int maxProf = 0 ;

        for(int i=n-2; i >=0 ; i--){
            if(A[i] > max){
                max = A[i];
            }
            if(maxProf < max - A[i]){
                maxProf = max - A[i];
            }
        }
        return maxProf;
    }
    public static void main(String[] args) {
        int[] prices = {7, 1, 5, 3, 6, 4};
        System.out.println("Maximum Profit: " + maxProfit(prices));
    }
    
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: