Given an input integer array arr, where each array element represents the price of the stock on that day, find the maximum achievable profits by buying and selling that stock within the price range of the elements of the array.
| Input Parameters |
Expected outputs |
| Stock price daily price arr={100,110,120,115,110,100,90}
|
20
|
| Stock price daily price arr={100,90,70,80,90,110}
|
40
|
| Stock price daily price arr={100,100,100,100}
|
0
|
| Stock price daily price arr={ 100,120,90,115,125,95,130}
|
40
|
| The java method should accept following input parameters: arr (int array). The array should contain day-wise stock price of a stock.
|
| We need a minimum price at which we will buy stock, so initialize an int variable minStockPrice. Set it to max integer value possible in java.
|
| Initialize two int variables maxProfit and todaysProfit to 0.
|
| Iterate through the array of daily prices using a for loop:
Set todaysProfit=arr[idx]- minStockPrice, to get profit we will get if the stock is sold at the price in the current index in arr.
If todaysProfit > maxProfit, set maxProfit to todaysProfit, this way maxProfit will hold the greatest possible profit we can earn.
If stock price at current index is less than minStockPrice, then set minStockPrice to today’s stock price, this way minStockPrice will hold the minimum possible price of stocks in arr.
|
| After the loop ends return maxProfit , as this is the max profit that be earned based on the day wise stock prices in the array.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayStockMarketMaxProfit {
public int arrayStockMarketMaxProfit(int[] arr) throws Exception{
int minStockPrice=Integer.MAX_VALUE;
int maxProfit=0;
int todaysProfit=0;
for (int idx=0; idx < arr.length; idx++) {
todaysProfit=arr[idx]-minStockPrice;
if (todaysProfit > maxProfit) {
maxProfit=todaysProfit;
}
if (arr[idx] < minStockPrice) {
minStockPrice=arr[idx];
}
}
return maxProfit;
}
public static void main(String[] args) {
ArrayStockMarketMaxProfit ap=new ArrayStockMarketMaxProfit();
int maxProfit=0;
try {
int[] intArray1 = {100,110,120,115,110,100,90};
printArraySummary(intArray1, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray1);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
int[] intArray2 = {100,90,70,80,90,110};
printArraySummary(intArray2, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray2);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
int[] intArray3 = {100,130,150};
printArraySummary(intArray3, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray3);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
int[] intArray4 = {190,170,160,120,115};
printArraySummary(intArray4, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray4);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
int[] intArray5 = {100};
printArraySummary(intArray5, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray5);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
int[] intArray6 = {};
printArraySummary(intArray6, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray6);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
int[] intArray7 = {100,100,100,100};
printArraySummary(intArray7, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray7);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
int[] intArray8 = {100,120,90,115,125,95,130};
printArraySummary(intArray8, "Stock Prices");
maxProfit=ap.arrayStockMarketMaxProfit(intArray8);
System.out.println("\n\n Maximum profit that can be earned is : "+ maxProfit+"\n");
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}