import java.util.*;
import java.io.*;
class GFG
{
static int maxProfit(int price[], int start, int end)
{
if(end <= start)
return 0;
int profit = 0;
for(int i = start; i < end; i++)
{
for(int j = i + 1; j <= end; j++)
{
if(price[j] > price[i])
{
int curr_profit = price[j] - price[i]
+ maxProfit(price, start, i - 1)
+ maxProfit(price, j + 1, end);
profit = Math.max(profit, curr_profit);
}
}
}
return profit;
}
public static void main(String args[])
{
int arr[] = {1, 5, 3, 8, 12}, n = 5;
System.out.println(maxProfit(arr, 0, n-1));
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter