Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Become a Premium Member and unlock a new, free course in leading technologies each month.

2K

Solutions

3K

Contributors

Puzzles and riddles are statements and questions framed such that they require ingenuity and imagination to resolve. Most are mathematical conundrums, but can also include the arcane, trivial or simply funny.

Share tech news, updates, or what's on your mind.

Here's an interesting coding puzzle:

A trader can perform two buys and two sells in a day of a given stock. An array of positive integers represents the share price of this stock at different times of the day (index i = 0 represents hour 1, i=1 represents hour 2, etc). The trader must always sell any current shares he has before he buys more shares. Write a function that determines the maximum profit the trader can make by buying and selling stock for a given day.

The solution should have time complexity O(n).

A trader can perform two buys and two sells in a day of a given stock. An array of positive integers represents the share price of this stock at different times of the day (index i = 0 represents hour 1, i=1 represents hour 2, etc). The trader must always sell any current shares he has before he buys more shares. Write a function that determines the maximum profit the trader can make by buying and selling stock for a given day.

```
int maximizeProfit(int[] prices) {
// Your code here
}
ex:
[10, 22, 5, 75, 65, 80]
The maximum profit is 87: buy at 10 sell at 22, buy at 5, sell at 80.
```

The solution should have time complexity O(n).

0 Comments

Get HTML5 Certified
Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

N is a number that represents n boxes and n workers. All boxes are initially closed. Worker 1 goes through and toggles every box open. Worker 2 goes through and toggles every 2nd box (closed if open, open if closed). In the end, every worker i âŠ‚ N has toggled every ith box opened or closed. Print the boxes that remain open at the end.

```
public void printOpenBoxes(n)
{
// Implementation here
}
```

2K

Solutions

3K

Contributors

Puzzles and riddles are statements and questions framed such that they require ingenuity and imagination to resolve. Most are mathematical conundrums, but can also include the arcane, trivial or simply funny.

Top Experts In

Puzzles / Riddles

No Top Experts for this time period. Answer questions to earn the title!