Let's test your knowledge. Click the correct answer from the options.
What is the time complexity of the below brute force solution?
1function stockOptimizer(prices) {
2 let maxDiff = 0;
3 for (let i = 0; i < prices.length; i++) {
4 for (let j = i + 1; j < prices.length; j++) {
5 let newDiff = prices[j] - prices[i];
6
7 if (newDiff > maxDiff) {
8 maxDiff = newDiff;
9 }
10 }
11 }
12
13 return maxDiff;
14}
15
16console.log(stockOptimizer([2, 5, 15, 9]));
Click the option that best answers the question.
- O(n)
- O(n^2)
- O(n^3)
- O(1)