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 = 0 ; 
        for(int i=0 ; i<n-1 ; i++){  // Buying
            for(int j=i+1; j < n ; j++){ // Selling 
                int profit = A[j] - A[i] ; // Buy - Sell
                if(profit > max){
                    max = profit;
                }
            }
        }
        return max; 
    }
    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: