Skip to content

Instantly share code, notes, and snippets.

@axayjha
Created May 16, 2025 14:59
Show Gist options
  • Save axayjha/d180f64b3e0943d2057d42eed2ac3c1e to your computer and use it in GitHub Desktop.
Save axayjha/d180f64b3e0943d2057d42eed2ac3c1e to your computer and use it in GitHub Desktop.
q24_new
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