Want to solve Max Average **Stock Price Amazon OA?, **if yes then this article is for you.

We have research and collected a bundle of questions which was asked in Amazon OA in the year of 2022 and 2023. Today, we are going to see another problem from amazon oa called Stock Price which was asked in 2022, this questions might be still active in some of the Amazon OA so save all the solutions and lets see how can we solve this problem.

**Max Average Stock Price Amazon OA**

The interns at Amazon were asked to review the company’s stock value over a period. Given the stock prices of n months, the net price change for the i^{th} month is defined as the absolute difference between the average of stock prices for the first i months and for the remaining (n – i) months where 1≤ i <n. Note that these averages are rounded down to an integer.

Given an array of stock prices, find the month at which the net price change is minimum. If there are several such months, return the earliest month.

Note: The average of a set of integers here is defined as the sum of integers divided by the number of integers, rounded down to the nearest integer. For example, the average of [1, 2, 3, 4] is the floor of (1 + 2 + 3 + 4) / 4 = 2.5 and the floor of 2.5 is 2.

**Example**

stockPrice = [1, 3, 2, 3]

The minimum net price change is 0, and it occurs in the 2^{nd} month. Return 2.

**Function Description**

Complete the function *findEarliestMonth* the editor below.

*findEarliestMonth* has the following parameter:** int stockPrice[n]: **the stock prices

**Returns**

*int*: the earliest month in which the net price change is minimum

**Constraints**

- 2 ≤ n ≤ 10
^{5} - 1 ≤ stockPrice[i] ≤ 10
^{9}

**Sample Input**

```
STDIN FUNCTION
------ ----------
5 -> stockPrice[]
size n = 5
1 -> stockPrice = [1, 3, 2, 4, 5]
3
2
4
5
```

**Sample Output**

2

**Explanation**

The net price change can be calculated as:

- Month 1: [1] and [3, 2, 4, 5], their respective averages, rounded down = 1 and 3, net price change = 2
- Month 2: [1, 3] and [2, 4, 5], averages = 2 and 3, net price change = 1
- Month 3: [1, 3, 2] and [4, 5], averages = 2 and 4, net price change = 2
- Month 4: [1, 3, 2, 4] and [5], averages = 2 and 5, net price change = 3

The minimum net price change is 1, and it occurs at month 2.

**Sample Input 1**

```
STDIN FUNCTION
------ ----------
6 -> stockPrice[]
size n = 6
1 -> stockPrice = [1, 1, 1, 1, 1, 1]
1
1
1
1
1
```

**Sample Output 1**

1

**Explanation**

Since all the stock prices are all the same, the average is always 1 and the net price change is always 0. The earliest month this occurs is 1.

**SOLUTION**

**Program: Max Average Stock Price Amazon OA Solution** in Python

To solve this problem, we need to iterate through all possible values of i from 1 to n-1, calculate the net price change for that value of i, and keep track of the minimum value and the corresponding month. To calculate the net price change, we can first calculate the averages of the first i months and the remaining (n-i) months, and then subtract the smaller average from the larger one.

**Here’s the step-by-step algorithm:**

- Initialize min_month = 1 and min_net_price_change = infinity
- For i = 1 to n-1 do the following:
- a. Calculate the average of the first i months, rounded down to the nearest integer.
- b. Calculate the average of the remaining (n-i) months, rounded down to the nearest integer.
- c. Calculate the net price change as the absolute difference between the two averages.
- d. If the net price change is less than min_net_price_change, update min_net_price_change to the new value and min_month to i.
- e. If the net price change is equal to min_net_price_change, update min_month to the minimum of i and min_month.

- Return min_month.

```
def findEarliestMonth(stockPrice):
n = len(stockPrice)
min_month = 1
min_net_price_change = float('inf')
for i in range(1, n):
first_avg = sum(stockPrice[:i]) // i
second_avg = sum(stockPrice[i:]) // (n-i)
net_price_change = abs(first_avg - second_avg)
if net_price_change < min_net_price_change:
min_net_price_change = net_price_change
min_month = i
elif net_price_change == min_net_price_change:
min_month = min(min_month, i)
return min_month
```

**Program: Max Average Stock Price Amazon OA Solution** in C++

**Explanation:**

- The function takes a vector of integers stockPrice as input and returns an integer.
- The function first gets the size of the stockPrice vector using the size method.
- It initializes two variables: min_month to 1 (the earliest month) and min_net_price_change to the maximum integer value INT_MAX (the maximum possible net price change).
- It then iterates over all possible months (1 to n-1) using a for loop.
- For each month i, it calculates the sum of the stock prices for the first i months using the accumulate function from the library.
- It also calculates the sum of the stock prices for the remaining (n-i) months by using the begin and end iterators of the stockPrice vector.
- It calculates the average stock price for the first i months by dividing first_sum by i.
- It also calculates the average stock price for the remaining (n-i) months by dividing second_sum by (n-i).
- It calculates the net price change for this month by taking the absolute difference between the two averages.
- It checks if this month has the minimum net price change so far.
- If so, it updates the min_net_price_change and min_month variables to the net price change and the current month i, respectively.
- If there are multiple months with the same minimum net price change, it chooses the earliest one by using the min function.
- After iterating over all possible months, it returns the

```
#include <iostream>
#include <vector>
#include <numeric>
#include <climits>
using namespace std;
int findEarliestMonth(vector<int> stockPrice) {
// Get the size of the stockPrice vector
int n = stockPrice.size();
// Initialize variables to hold the minimum net price change and the earliest month
int min_month = 1, min_net_price_change = INT_MAX;
// Iterate over all possible months (1 to n-1)
for (int i = 1; i < n; i++) {
// Calculate the sum of the stock prices for the first i months
int first_sum = accumulate(stockPrice.begin(), stockPrice.begin()+i, 0);
// Calculate the sum of the stock prices for the remaining (n-i) months
int second_sum = accumulate(stockPrice.begin()+i, stockPrice.end(), 0);
// Calculate the average stock price for the first i months (rounded down)
int first_avg = first_sum / i;
// Calculate the average stock price for the remaining (n-i) months (rounded down)
int second_avg = second_sum / (n-i);
// Calculate the net price change for this month
int net_price_change = abs(first_avg - second_avg);
// Check if this month has the minimum net price change so far
if (net_price_change < min_net_price_change) {
// Update the minimum net price change and earliest month
min_net_price_change = net_price_change;
min_month = i;
} else if (net_price_change == min_net_price_change) {
// If there are multiple months with the same minimum net price change, choose the earliest one
min_month = min(min_month, i);
}
}
// Return the earliest month with the minimum net price change
return min_month;
}
```

**Program: Max Average Stock Price Amazon OA Solution** in Java

The function findEarliestMonth takes an array of stock prices as input and returns the earliest month in which the net price change is minimum.

The function initializes the minimum month and minimum net price change to the first month and a large value, respectively.

Then, the function loops through each possible month to split the array. For each split, it calculates the averages of the left and right subarrays by summing the elements in each subarray and dividing by the number of elements. The function uses integer division (/) to round down to the nearest integer, as specified in the problem statement.

After calculating the averages, the function calculates the net price change for this split by taking the absolute difference between the two averages.

Finally, the function updates the minimum net price change and earliest month if necessary. If the net price change is smaller than the current minimum, the minimum net price change and earliest month are updated. If the net price change is equal to the current minimum, the function takes the earliest month.

Once the loop is finished, the function returns the earliest month. Note that the function adds 1 to the month index since the array is 0-indexed but the months are 1-indexed.

```
public static int findEarliestMonth(int[] stockPrice) {
int n = stockPrice.length;
int minMonth = 1;
int minNetPriceChange = Integer.MAX_VALUE;
// Loop through each possible month to split the array
for (int i = 1; i < n; i++) {
// Calculate the averages of the left and right subarrays
int leftSum = 0;
for (int j = 0; j < i; j++) {
leftSum += stockPrice[j];
}
int leftAvg = leftSum / i;
int rightSum = 0;
for (int j = i; j < n; j++) {
rightSum += stockPrice[j];
}
int rightAvg = rightSum / (n - i);
// Calculate the net price change for this split
int netPriceChange = Math.abs(leftAvg - rightAvg);
// Update the minimum net price change and earliest month if necessary
if (netPriceChange < minNetPriceChange) {
minNetPriceChange = netPriceChange;
minMonth = i + 1; // Add 1 since the array is 0-indexed but the months are 1-indexed
} else if (netPriceChange == minNetPriceChange) {
minMonth = Math.min(minMonth, i + 1);
}
}
return minMonth;
}
```

**Amazon OA 2023 Questions** **with Solution**

- Shopping Patterns Solution Amazon OA 2023
- Reorder Data in Log Files Solution Amazon OA 2023
- Top K Frequent Words Solution Amazon OA 2023
- Trees Height Solution Amazon OA SDE 2023
- Counting Binary Substrings Amazon OA 2023
- Grid Connections Amazon OA 2023
- Shipment Imbalance Amazon OA 2023
- Max Profit Amazon OA 2023
- Find Lowest Price Amazon OA 2023
- Decode String Frequency Amazon OA 2023
- Simple Cipher Amazon OA 2023
- Valid Discount Coupons Amazon OA 2023 Solution
- Count Maximum Teams Amazon OA 2023
- Minimum Coin Flips Amazon OA 2023
- Robot Bounded In Circle Amazon OA 2023
- Shopping Options Amazon OA 2023 Solution
- Fill The Truck Maximum Units on a Truck Amazon OA Solution
- Maximize Score After N Operations Number Game Solution Amazon OA 2023
- Slowest Key Amazon OA 2023 Solution
- Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
- Split String Into Unique Primes Amazon OA 2023 Solution
- Storage Optimization Amazon OA 2023 Solution
- Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
- Autoscale Policy Utilization Check Amazon OA 2023
- Optimal Utilization Solution Amazon OA 2023
- Merge Two Sorted Lists Solution Amazon OA 2023
- Two Sum Unique Pairs Solution Amazon OA 2023
- Amazon Music Pairs Amazon OA 2023 Solution
- Class Grouping Amazon OA 2023 Solution
- Find Max products Amazon OA 2023 Solution
- Get encrypted number Amazon OA 2023 Solution
- Find Total Imbalance Amazon OA 2023 Solution
- Find Total Power Amazon OA 2023 Solution

Nice

I need solution fast

I need solution now..!!

I want solution now…!!

We can be notified when the solution is posted?