-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuy_sell_stocks.java
More file actions
32 lines (27 loc) · 957 Bytes
/
buy_sell_stocks.java
File metadata and controls
32 lines (27 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
public class buy_sell_stocks {
public static void profit(int arr[]) {
//calculate buying price and selling price at every day
int buy_price[] = new int[arr.length];
buy_price[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
buy_price[i] = Math.min(buy_price[i - 1], arr[i]);
}
int selling_price[] = new int[arr.length];
for (int i = 1; i < arr.length; i++) {
selling_price[i] = arr[i];
}
int max_profit = Integer.MIN_VALUE;
for (int i = 1; i < arr.length; i++) {
int profit = selling_price[i] - buy_price[i];
if (profit > max_profit) {
max_profit = profit;
}
}
System.out.println(max_profit);
}
public static void main(String[] args){
int stocks_data[]={7,1,5,3,6,4};
profit(stocks_data);
}
}