Find Earliest Month π
Learn this problemProblem statement
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 ith 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.
Function
findEarliestMonth(stockPrice: int[]) β int
Complete the function findEarliestMonth in the editor.
findEarliestMonth has the following parameter:
int[] stockPrice: an array of integers representing the stock prices
Returns
int: the earliest month at which the net price change is minimum
Examples
Example 1
stockPrice = [1, 3, 2, 3]return = 2
Example 2
stockPrice = [1, 3, 2, 4, 5]return = 2[1] and [3, 2, 4, 5], their respective averages, rounded down = 1 and 3, net price change = 2[1, 3] and [2, 4, 5], averages = 2 and 3, net price change = 1[1, 3, 2] and [4, 5], averages = 2 and 4, net price change = 2[1, 3, 2, 4] and [5], averages = 2 and 5, net price change = 31, and it occurs at month 2 :D.More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW Β· Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA Β· Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW Β· Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW Β· Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW Β· Seen Jul 2026
- Merge IntervalsOA Β· Seen Jul 2026
- Sort Bug Report FrequenciesOA Β· Seen Jul 2026
- Drone Delivery RouteOA Β· Seen Jul 2026