BackmediumArraysUberRazorpay

Maximal Stock Return Solution

Problem Statement

Given an array of integers stockPrices representing the current prices of stocks and an array of floats growthRates representing their respective growth rates, determine the indices of the stocks that will yield the highest returns if you select at most one stock.

Example 1
Input
[10, 20, 30, 40, 50], [0.1, 0.2, 0.3, 0.4, 0.5]
Output
[4]

Explanation: Step-by-step: with input X, we first calculate the returns for each stock by multiplying the price with the growth rate. Then, we find the maximum return and its index. Since there is only one stock with the maximum return, we return its index.

Example 2
Input
[10, 20, 30, 40, 50], [0.1, 0.1, 0.1, 0.1, 0.1]
Output
[0, 1, 2, 3, 4]

Explanation: Step-by-step: with input X, we first calculate the returns for each stock by multiplying the price with the growth rate. Then, we find the maximum return and its indices. Since all stocks have the same maximum return, we return all their indices.

Constraints

  • The input array will contain at least 1 and at most 20 stock-price pairs.
  • Each stock price will be a positive integer between 1 and 1000.
  • Each growth rate will be a decimal value between 0.01 and 0.1.
  • The output should be an array of at most one index corresponding to the selected stock.
  • The budget for investment can be considered unlimited for simplicity.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximal Stock Return — Problem Statement & Solution Guide

ArraysMediumGeneral
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers stockPrices representing the current prices of stocks and an array of floats growthRates representing their respective growth rates, determine the indices of the stocks that will yield the highest returns if you select at most one stock.

Examples

Example 1

Input

[10, 20, 30, 40, 50], [0.1, 0.2, 0.3, 0.4, 0.5]

Output

[4]

Explanation: Step-by-step: with input X, we first calculate the returns for each stock by multiplying the price with the growth rate. Then, we find the maximum return and its index. Since there is only one stock with the maximum return, we return its index.

Example 2

Input

[10, 20, 30, 40, 50], [0.1, 0.1, 0.1, 0.1, 0.1]

Output

[0, 1, 2, 3, 4]

Explanation: Step-by-step: with input X, we first calculate the returns for each stock by multiplying the price with the growth rate. Then, we find the maximum return and its indices. Since all stocks have the same maximum return, we return all their indices.

Constraints

  • The input array will contain at least 1 and at most 20 stock-price pairs.
  • Each stock price will be a positive integer between 1 and 1000.
  • Each growth rate will be a decimal value between 0.01 and 0.1.
  • The output should be an array of at most one index corresponding to the selected stock.
  • The budget for investment can be considered unlimited for simplicity.

Optimal Approach & Strategy

The optimized approach involves iterating through the array once to find the stock with the highest growth rate, resulting in a linear time complexity of O(n).

Brute Force Approach

The brute force approach would involve comparing each stock's growth rate to every other stock, resulting in a highly inefficient solution.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function optimalInvestmentPortfolio(stocks) {
    let maxReturn = -Infinity;
    let maxReturnIndices = [];
    for (let i = 0; i < stocks.length; i++) {
        let returnOnInvestment = stocks[i][0] * stocks[i][1];
        if (returnOnInvestment > maxReturn) {
            maxReturn = returnOnInvestment;
            maxReturnIndices = [i];
        } else if (returnOnInvestment === maxReturn) {
            maxReturnIndices.push(i);
        }
    }
    return maxReturnIndices;
}

Asked in Top Tech Interviews

UberRazorpay

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.