-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestTimetoBuyandSellStock.cpp
More file actions
72 lines (58 loc) · 1.78 KB
/
Copy pathBestTimetoBuyandSellStock.cpp
File metadata and controls
72 lines (58 loc) · 1.78 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
61
62
63
64
65
66
67
68
69
70
71
Given an array prices[] of length N, representing the prices of the stocks on different days, the task is to find the maximum profit possible for buying and selling the stocks on different days using transactions where at most one transaction is allowed.
Note: Stock must be bought before being sold.
Examples:
Input: prices[] = {7, 1, 5, 3, 6, 4]
Output: 5
Explanation:
The lowest price of the stock is on the 2nd day, i.e. price = 1. Starting from the 2nd day, the highest price of the stock is witnessed on the 5th day, i.e. price = 6.
Therefore, maximum possible profit = 6 – 1 = 5.
// C++ code for the above approach
#include <iostream>
using namespace std;
int maxProfit(int prices[], int n)
{
int buy = prices[0], max_profit = 0;
for (int i = 1; i < n; i++) {
// Checking for lower buy value
if (buy > prices[i])
buy = prices[i];
// Checking for higher profit
else if (prices[i] - buy > max_profit)
max_profit = prices[i] - buy;
}
return max_profit;
}
// Driver Code
int main()
{
int prices[] = { 7, 1, 5, 6, 4 };
int n = sizeof(prices) / sizeof(prices[0]);
int max_profit = maxProfit(prices, n);
cout << max_profit << endl;
return 0;
}
// Java code for the above approach
class GFG {
static int maxProfit(int prices[], int n)
{
int buy = prices[0], max_profit = 0;
for (int i = 1; i < n; i++) {
// Checking for lower buy value
if (buy > prices[i])
buy = prices[i];
// Checking for higher profit
else if (prices[i] - buy > max_profit)
max_profit = prices[i] - buy;
}
return max_profit;
}
// Driver Code
public static void main(String args[])
{
int prices[] = { 7, 1, 5, 6, 4 };
int n = prices.length;
int max_profit = maxProfit(prices, n);
System.out.println(max_profit);
}
}
// This code is contributed by Lovely Jain