📖 Scenario: You are helping a friend who wants to understand how to find the best time to buy and sell stocks to maximize profit. They have a list of stock prices for consecutive days and want to learn how to find the best single buy-sell pair.
🎯 Goal: Build a simple program that finds the maximum profit from buying and selling a stock once, given a list of daily prices.
📋 What You'll Learn
Create a list of integers called prices with exact values: 7, 1, 5, 3, 6, 4
Create an integer variable called maxProfit and set it to 0
Use a for loop with variable i to iterate over prices starting from index 1
Inside the loop, update minPrice and calculate maxProfit accordingly
Print the value of maxProfit
💡 Why This Matters
🌍 Real World
This project helps understand how to analyze stock prices to make smart buy and sell decisions, a common task in finance and investing.
💼 Career
Knowing how to solve buy and sell stock problems is useful for software roles in finance, trading platforms, and data analysis.
Progress0 / 4 steps
1
Create the stock prices list
Create a list of integers called prices with these exact values: 7, 1, 5, 3, 6, 4.
DSA C#
Hint
Use List<int> and initialize it with the exact values inside curly braces.
2
Set up the maxProfit and minPrice variables
Create an integer variable called maxProfit and set it to 0. Create another integer variable called minPrice and set it to the first element of prices.
DSA C#
Hint
Set maxProfit to zero and minPrice to the first price in the list.
3
Calculate the maximum profit using a loop
Use a for loop with variable i starting from 1 to iterate over prices. Inside the loop, update minPrice to the smaller of minPrice and prices[i]. Then update maxProfit to the larger of maxProfit and prices[i] - minPrice.
DSA C#
Hint
Use for (int i = 1; i < prices.Count; i++) and inside update minPrice and maxProfit using if and else if.
4
Print the maximum profit
Print the value of maxProfit using Console.WriteLine(maxProfit);.
DSA C#
Hint
Use Console.WriteLine(maxProfit); to show the result.
Practice
(1/5)
1. What is the main goal when solving the Buy and Sell Stocks problem variants?
easy
A. Minimize the number of transactions regardless of profit
B. Find the stock price on the last day only
C. Maximize the profit by choosing the best days to buy and sell stocks
D. Calculate the average stock price over all days
Solution
Step 1: Understand the problem objective
The problem asks to maximize profit by buying and selling stocks on certain days.
Step 2: Identify the key action
Choosing the best days to buy low and sell high maximizes profit.
Final Answer:
Maximize the profit by choosing the best days to buy and sell stocks -> Option C
Quick Check:
Goal = Maximize profit [OK]
Hint: Focus on profit maximization, not just transactions [OK]
Common Mistakes:
Confusing profit with number of transactions
Ignoring the buy before sell rule
Trying to find average price instead of max profit
2. Which of the following C# code snippets correctly initializes an array of stock prices for the Buy and Sell Stocks problem?
easy
A. int prices[] = new int[6] {7, 1, 5, 3, 6, 4};
B. int prices = {7, 1, 5, 3, 6, 4};
C. int[] prices = (7, 1, 5, 3, 6, 4);
D. int[] prices = new int[] {7, 1, 5, 3, 6, 4};
Solution
Step 1: Check correct array initialization syntax in C#
In C#, arrays are initialized with int[] prices = new int[] { ... };.
Step 2: Verify each option
int[] prices = new int[] {7, 1, 5, 3, 6, 4}; uses correct syntax. The other options have invalid syntax: one declares a single int without array type, another uses parentheses instead of braces, and the last has incorrect array declaration style.
Final Answer:
int[] prices = new int[] {7, 1, 5, 3, 6, 4}; -> Option D
Hint: Add profit only when price increases day-to-day [OK]
Common Mistakes:
Starting loop at wrong index
Adding profit on price drops
Ignoring empty or single-element arrays
5. Given the prices array {3, 3, 5, 0, 0, 3, 1, 4} and a limit of at most two transactions, what is the maximum profit achievable?
hard
A. 8
B. 7
C. 5
D. 6
Solution
Step 1: Understand the two transactions limit
We can buy and sell at most twice to maximize profit.
Step 2: Calculate max profit by splitting days
First transaction: buy at 3, sell at 5 (profit 2). Second transaction: buy at 0, sell at 4 (profit 4). Total profit = 2 + 4 = 6. But better transactions are buy at 0 sell at 3 (profit 3), then buy at 1 sell at 4 (profit 3), total 6. However, the maximum profit is actually 8 by buying at 0 and selling at 5 (profit 5), then buying at 1 and selling at 4 (profit 3).
Final Answer:
8 -> Option A
Quick Check:
Two transactions max profit = 8 [OK]
Hint: Split array to find best two transaction profits [OK]
Common Mistakes:
Counting more than two transactions
Not choosing optimal buy/sell days
Adding profits incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.