-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForce.java
More file actions
60 lines (50 loc) · 1.35 KB
/
BruteForce.java
File metadata and controls
60 lines (50 loc) · 1.35 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bruteforce;
/**
*
* @author Vinayak
*/
public class BruteForce {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] price = {100,113,110,85,105,102,86,63,81,101,94,106,101,79,94,90,97};
int max_sum = 0 ,max_buy_date = 0,max_sell_date = 0;
int buy_date , sell_date , sum , result ;
int len = price.length;
int Array[]= new int[len-1];
for(int i=0;i<len-1;i++)
{
Array[i] = price[i+1] - price[i];
}
for(int i = 0; i < Array.length-1; i++)
{
sum = Array[i] + Array[i+1];
result = sum;
buy_date = i;
sell_date = i+1;
for(int j=i+2; j<Array.length-1; j++)
{
result = result + Array[j];
if(sum < result)
{
sum = result;
sell_date = j;
}
}
if(sum > max_sum)
{
max_sum = sum;
max_sell_date = sell_date + 1;
max_buy_date = buy_date;
}
}
System.out.println("The max profit is " + max_sum);
System.out.println("Buy On day " + max_buy_date + " and sell On day " + max_sell_date);
}
}