Created
May 16, 2025 14:59
-
-
Save axayjha/d180f64b3e0943d2057d42eed2ac3c1e to your computer and use it in GitHub Desktop.
q24_new
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getTopStocks(stocks, prices) { | |
// Step 1: Calculate average for each stock | |
const n = stocks.length; | |
const averages = []; | |
for (let i = 0; i < n; i++) { | |
let sum = 0; | |
for (let j = 0; j < prices.length; j++) { | |
sum += prices[j][i]; | |
} | |
const avg = sum / prices.length; | |
averages.push([stocks[i], avg]); | |
} | |
// Step 2: Sort by average in descending order | |
averages.sort((a, b) => b[1] - a[1]); | |
// Step 3: Extract top 3 stock names | |
return averages.slice(0, 3).map(pair => pair[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment