-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxprofit_trading.cs
77 lines (60 loc) · 1.62 KB
/
maxprofit_trading.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
// To execute C#, please define "static void Main" on a class
// named Solution.
class Solution
{
static void Main(string[] args)
{
int[] arr = new int[5]{7, 1, 5, 3, 6};
int p = maxdiff(arr);
Console.WriteLine(p);
maxdiffeff(arr);
}
public static void maxdiffeff(int[] arr)
{
int length = arr.Length;
int max_diff = 0;
int min_elem = arr[0];
int i=0;
int buy=0;
int sell =0;
for(i=0; i< length; i++)
{
if(arr[i] - min_elem > max_diff)
{
max_diff = arr[i] - min_elem;
sell = i;
}
if(arr[i] < min_elem)
{
min_elem = arr[i];
buy = i;
}
}
Console.WriteLine("buy on day: " + buy + " sell on day: " + sell);
Console.WriteLine(max_diff);
}
public static int maxdiff(int[] arr)
{
int length = arr.Length;
int max_diff = 0;
int buy = 0;
int sell =0;
int i =0;
int j = 0;
for(i=0; i< length; i++)
{
for(j=i+1; j<length; j++)
{
if(arr[j] -arr[i] > max_diff)
{
max_diff = arr[j] - arr[i];
buy = i;
sell = j;
}
}
}
Console.WriteLine("buy on day: " + buy + " sell on day: " + sell);
return max_diff;
}
}